Template mixin Instantiation

Jorge Lima via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed May 25 00:45:32 PDT 2016


Can someone explain me why in the following code the alias 
parameter expands differently for first and second Instantiation?

module main;
import std.stdio;
import std.conv;

struct Cascade {
     this(int a, immutable(Cascade)[] b)
     {
         f1 = a;
         f2 = b;
     }
     this(int a)
     {
         f1 = a;
         f2 = null;
     }
     int f1;
     immutable(Cascade)[] f2;
}

immutable Cascade [] array1 = [
     Cascade(1),
     Cascade(2),
     Cascade(3),
     Cascade(3)
];

mixin template StaticArr(alias a) {
     immutable(typeof(a[0]))[a.length] array = a;
     string repr = a.stringof;
     void printIt()
     {
         writeln(repr);
     }
}

int main()
{
     mixin StaticArr!(array1) x;
     mixin StaticArr!([
         Cascade(10),
         Cascade(20, array1)]) y;

     x.printIt();
     y.printIt();

     return 0;
}

Running the program prints:
array1
[Cascade(10, null), Cascade(20, [Cascade(1, null), Cascade(2, 
null), Cascade(3,
null), Cascade(3, null)])]

I can understand that array1 is not expanded to its value 
representation in the first call, but why is then when passed as 
an argument to the Constructor of the literal argument in the 
second call? Am I missing something obvious?

Thanks,
Jorge



More information about the Digitalmars-d-learn mailing list