On Sun, Mar 7, 2010 at 19:53, Ellery Newcomer
<ellery-newcomer@utulsa.edu> wrote:
Totally awesome! Thanks a float.infinity!
I remember spending *weeks* (well, OK, *hours*) trying to find a way to do such a mapping after trying the first foreach, and getting some recursive template to work... Only to find this static foreach trick, looking at Phobos code (for D2) some month after. It sent ripples in all my code...
As a side note, it works for any tuple: those created by .tupleof, but also the variadic template parameters, the expansion of a std.typecons.Tuple (D2), those created by templates, etc. So you can do:
void reinitialize(T...)(ref T ts) // Variadic template
{
foreach(i, Type; T) ts[i] = T[i].init; // iterating on the components of T.
}
int a = 1;
char c = 'a';
string s = "abc";
reinitialize(a,c,s);
assert(a = 0); // int.init
assert(c = char.init); // is there a literal for char.init? '' doesn't work.
assert(s="");
Philippe