On Wednesday, 2 September 2020 at 03:52:55 UTC, JG wrote:
Thank you all for the interesting suggestions.
Still thinking about this from time to time.
Other than the suggestions given, this is what I have
been playing around with.
---------------------------------
import std.stdio;
import std.typecons : tuple;
mixin template assignTuple(alias vars, alias tupleFunc)
{
import std.conv : to;
auto tmp = tupleFunc();
static foreach (i, var ; vars)
{
mixin("auto " ~ var ~ " = tmp[" ~ i.to!string ~ "];");
}
}
auto f(int n) { return tuple(n, n+1, n+2, "A string here"); }
void main()
{
mixin assignTuple!(["x","y","z", "str"],()=>f(3));
writeln(x," ",y," ", z," \'",str,"\'");
}
---------------------------------
produces
---------------------------------
3 4 5 'A string here'
---------------------------------
I have a few questions:
1. Is the above code "bad" for some reason?
2. Is there a way of "hiding" tmp used in the mixin, so that it
is not visible in main?