Ddoc $(SPEC_S Template Mixins, A $(I TemplateMixin) takes an arbitrary set of declarations from the body of a $(I TemplateDeclaration) and inserts them into the current context. $(GRAMMAR $(GNAME TemplateMixinDeclaration): $(V2 $(B mixin)) $(B template) $(TEMPLATEIDENTIFIER) $(B $(LPAREN)) $(TEMPLATEPARAMETERLIST) $(B $(RPAREN)) $(V2 $(GLINK Constraint)$(SUB $(I opt))) $(B {) $(LINK2 module.html#DeclDefs, DeclDefs) $(B }) $(GNAME TemplateMixin): $(B mixin) $(TEMPLATEIDENTIFIER) $(B ;) $(B mixin) $(TEMPLATEIDENTIFIER) $(GLINK MixinIdentifier) $(B ;) $(B mixin) $(TEMPLATEIDENTIFIER) $(B !$(LPAREN)) $(TEMPLATEARGUMENTLIST) $(B $(RPAREN)) $(B ;) $(B mixin) $(TEMPLATEIDENTIFIER) $(B !$(LPAREN)) $(TEMPLATEARGUMENTLIST) $(B $(RPAREN)) $(GLINK MixinIdentifier) $(B ;) $(GNAME MixinIdentifier): $(I Identifier) ) $(P A $(I TemplateMixin) can occur in declaration lists of modules, classes, structs, unions, and as a statement. The $(I TemplateIdentifier) refers to a $(I TemplateDeclaration). If the $(I TemplateDeclaration) has no parameters, the mixin form that has no !($(I TemplateArgumentList)) can be used. ) $(P 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 $(SINGLEQUOTE boilerplate) code, as well as for creating templated nested functions, which is not possible with template instantiations. ) ------ $(V2 mixin) template Foo() { int x = 5; } $(B mixin Foo;) struct Bar { $(B 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 { $(B 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: ------ $(V2 mixin) template Foo(T) { T x = 5; } $(B mixin Foo!(int);) // create x of type int ------ Mixins can add virtual functions to a class: ------ $(V2 mixin) template Foo() { void func() { writefln("Foo.func()"); } } class Bar { $(B 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; $(V2 mixin) template Foo() { int abc() { return y; } } void test() { int y = 8; $(B mixin Foo;) // local y is picked up, not global y assert(abc() == 8); } ------ Mixins can parameterize symbols using alias parameters: ------ $(V2 mixin) template Foo(alias b) { int abc() { return b; } } void test() { int y = 8; $(B 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: ------ $(V2 mixin) 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, $(B delegate { foo(); }) ); duff_loop(); // executes foo() 10 times } ------