Mixin Templates Over-restricted?

yaz yazan.dabain at gmail.com
Sat May 25 11:40:05 PDT 2013


On Saturday, 25 May 2013 at 18:28:09 UTC, Diggory wrote:
>
> I think you can do it using a string mixin instead:
> enum Test = `writeln("Hello D People!")`
>
> void main() {
>     mixin(Test);
> }
>
> The answer to your question is probably that D has to know the 
> context for a template mixing at the point where it is declared 
> rather than where it is used.
>
> If non-declarations were allowed the semantic meaning of the 
> template mixin would depend on the way it was used, and that's 
> not allowed.
>
> I could also be completely wrong of course :P

The whole point of mixin templates is that they are evaluated 
within the scope where the mixin appears. Actually, an example of 
this effect (copied from the spec and modified slightly), where 
the semantic meaning of the template changes, compiles correctly.


mixin template Foo() {
   auto test() { return y; }
}

void test1() {
   int y = 1;
   mixin Foo;
   assert(test() == 1);
}

void test2() {
   string y = "hello";
   mixin Foo;
   assert(test() == "hello");
}

void main() {
   test1();
   test2();
}


More information about the Digitalmars-d-learn mailing list