www.digitalmars.com

D Programming Language 1.0


Last update Sun Dec 30 20:34:44 2012

Template Mixins

A TemplateMixin takes an arbitrary set of declarations from the body of a TemplateDeclaration and inserts them into the current context.
TemplateMixinDeclaration:
     template TemplateIdentifier ( TemplateParameterList ) 
	    { DeclDefs }

TemplateMixin:
    mixin TemplateIdentifier ;
    mixin TemplateIdentifier MixinIdentifier ;
    mixin TemplateIdentifier !( TemplateArgumentList ) ;
    mixin TemplateIdentifier !( TemplateArgumentList ) MixinIdentifier ;

MixinIdentifier:
    Identifier

A TemplateMixin can occur in declaration lists of modules, classes, structs, unions, and as a statement. The TemplateIdentifier refers to a TemplateDeclaration. If the TemplateDeclaration has no parameters, the mixin form that has no !(TemplateArgumentList) can be used.

Unlike a template instantiation, a template mixin's body is evaluated within the scope where the mixin appears, not where the template declaration is defined. It is analogous to cutting and pasting the body of the template into the location of the mixin. It is useful for injecting parameterized ‘boilerplate’ code, as well as for creating templated nested functions, which is not possible with template instantiations.

 template Foo() {
  int x = 5;
}

mixin Foo;

struct Bar {
  mixin Foo;
}

void test() {
  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
}
Mixins can be parameterized:
 template Foo(T) {
  T x = 5;
}

mixin Foo!(int);           // create x of type int
Mixins can add virtual functions to a class:
 template Foo() {
  void func() { writefln("Foo.func()"); }
}

class Bar {
  mixin Foo;
}

class Code : Bar {
  void func() { writefln("Code.func()"); }
}

void test() {
  Bar b = new Bar();
  b.func();      // calls Foo.func()

  b = new Code();
  b.func();      // calls Code.func()
}
Mixins are evaluated in the scope of where they appear, not the scope of the template declaration:
int y = 3;

 template Foo() {
  int abc() { return y; }
}

void test() {
  int y = 8;
  mixin Foo; // local y is picked up, not global y
  assert(abc() == 8);
}
Mixins can parameterize symbols using alias parameters:
 template Foo(alias b) {
  int abc() { return b; }
}

void test() {
  int y = 8;
  mixin Foo!(y);
  assert(abc() == 8);
}
This example uses a mixin to implement a generic Duff's device for an arbitrary statement (in this case, the arbitrary statement is in bold). A nested function is generated as well as a delegate literal, these can be inlined by the compiler:
 template duffs_device(alias id1, alias id2, alias s)
{
  void duff_loop()
  {
    if (id1 < id2)
    {
      typeof(id1) n = (id2 - id1 + 7) / 8;
      switch ((id2 - id1) % 8)
      {
        case 0: do { s();
        case 7:      s();
        case 6:      s();
        case 5:      s();
        case 4:      s();
        case 3:      s();
        case 2:      s();
        case 1:      s();
                } while (--n > 0);
        }
    }
  }
}

void foo() { writefln("foo"); }

void test() {
  int i = 1;
  int j = 11;

  mixin duffs_device!(i, j, delegate { foo(); } );
  duff_loop();  // executes foo() 10 times
}

Mixin Scope

The declarations in a mixin are ‘imported’ into the surrounding scope. If the name of a declaration in a mixin is the same as a declaration in the surrounding scope, the surrounding declaration overrides the mixin one:
int x = 3;

 template Foo() {
  int x = 5;
  int y = 5;
}

mixin Foo;
int y = 3;

void test() {
  writefln("x = %d", x);  // prints 3
  writefln("y = %d", y);  // prints 3
}
If two different mixins are put in the same scope, and each define a declaration with the same name, there is an ambiguity error when the declaration is referenced:
 template Foo() {
  int x = 5;
  void func(int x) { }
}

 template Bar() {
  int x = 4;
  void func() { }
}

mixin Foo;
mixin Bar;

void test() {
  writefln("x = %d", x); // error, x is ambiguous
  func();             // error, func is ambiguous
}

The call to func() is ambiguous because Foo.func and Bar.func are in different scopes.

If a mixin has a MixinIdentifier, it can be used to disambiguate:

int x = 6;

 template Foo() {
  int x = 5;
  int y = 7;
  void func() { }
}

 template Bar() {
  int x = 4;
  void func() { }
}

mixin Foo F;
mixin Bar B;

void test() {
  writefln("y = %d", y);     // prints 7
  writefln("x = %d", x);     // prints 6
  writefln("F.x = %d", F.x); // prints 5
  writefln("B.x = %d", B.x); // prints 4
  F.func();                  // calls Foo.func
  B.func();                  // calls Bar.func
}

Alias declarations can be used to overload together functions declared in different mixins:

 template Foo() {
  void func(int x) {  }
}

 template Bar() {
  void func() {  }
}

mixin Foo!() F;
mixin Bar!() B;

alias F.func func;
alias B.func func;

void main() {
  func();  // calls B.func
  func(1); // calls F.func
}

A mixin has its own scope, even if a declaration is overridden by the enclosing one:

int x = 4;

 template Foo() {
  int x = 5;
  int bar() { return x; }
}

mixin Foo;

void test() {
  writefln("x = %d", x);         // prints 4
  writefln("bar() = %d", bar()); // prints 5
}




Forums | Comments |  D  | Search | Downloads | Home