mixin template

Dan Killebrew nospam at gmail.com
Mon Jan 27 19:29:17 PST 2014


What is the difference between:
A
-----------
template Foo() {
   int x = 5;
}

B
-------------
mixin template Foo() {
   int x = 5;
}

The full example is from the first code sample on 
http://dlang.org/template-mixin.html  Both A and B compile and 
when run, have the exact same output. So how is 'mixin template' 
different from 'template'?

Full example, slightly modified to actually run:
import std.stdio;

  template Foo() {
//mixin template Foo() {
   int x = 5;
}

mixin Foo;

struct Bar {
   mixin Foo;
}

void main() {
   writefln("x = %d", x);  // prints 5
   {
     Bar b;
     int x = 3;

     writefln("b.x = %d", b.x); // prints 5
     writefln("x = %d", x);     // prints 3
     {
       mixin Foo;
       writefln("x = %d", x);   // prints 5
       x = 4;
       writefln("x = %d", x);   // prints 4
     }
     writefln("x = %d", x);     // prints 3
   }
   writefln("x = %d", x);       // prints 5
}


More information about the Digitalmars-d-learn mailing list