ATemplateMixin takes an arbitrary set of declarations from the body of aTemplateDeclaration and inserts them into the current context.
TemplateMixinDeclaration:mixintemplateIdentifierTemplateParametersConstraintopt{DeclDefsopt}TemplateMixin:mixinMixinTemplateNameTemplateArgumentsoptIdentifieropt;mixinIdentifier=MixinTemplateNameTemplateArgumentsopt;MixinTemplateName:.MixinQualifiedIdentifierMixinQualifiedIdentifierTypeof.MixinQualifiedIdentifierMixinQualifiedIdentifier:IdentifierIdentifier.MixinQualifiedIdentifierTemplateInstance.MixinQualifiedIdentifier
ATemplateMixin can occur in declaration lists of modules, classes, structs, unions, or as a statement.MixinTemplateName must refer to aTemplateDeclaration orTemplateMixinDeclaration. If theTemplateDeclaration requires no parameters,TemplateArguments can be omitted.
Unlike atemplate 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 into anested scope. It is useful for injecting parameterized ‘boilerplate’ code, as well as for creating templated nested functions, which is not always possible with template instantiations.
ATemplateMixinDeclaration is the same as aTemplateDeclaration, but can not be instantiated outside of aTemplateMixin.
int y = 3;mixintemplate Foo(){int abc() {return y; }}void test(){int y = 8;mixin Foo;// local y is picked up, not global yassert(abc() == 8);}
import std.stdio : writeln;mixintemplate Foo(){int x = 5;}mixin Foo;struct Bar{mixin Foo;}void main(){ writeln("x = ", x);// prints 5 { Bar b;int x = 3; writeln("b.x = ", b.x);// prints 5 writeln("x = ", x);// prints 3 {mixin Foo; writeln("x = ", x);// prints 5 x = 4; writeln("x = ", x);// prints 4 } writeln("x = ", x);// prints 3 } writeln("x = ", x);// prints 5}
Mixins can beparameterized:
mixintemplate Foo(T){ T x = 5;}mixin Foo!(int);// create x of type int
Mixins can parameterize symbols usingalias parameters:
mixintemplate 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:
import std.stdio : writeln;mixintemplate duffs_device(alias low,alias high,alias fun){void duff_loop() {if (low < high) {auto n = (high - low + 7) / 8;switch ((high - low) % 8) {case 0:do { fun();gotocase;case 7: fun();gotocase;case 6: fun();gotocase;case 5: fun();gotocase;case 4: fun();gotocase;case 3: fun();gotocase;case 2: fun();gotocase;case 1: fun();continue;default:assert(0,"Impossible"); }while (--n > 0); } } }}void main(){int i = 1;int j = 11;mixin duffs_device!(i, j,delegate { writeln("foo"); }); duff_loop();// executes foo() 10 times}
The declarations in a mixin are placed in a nested scope and then ‘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:
import std.stdio : writeln;int x = 3;mixintemplate Foo(){int x = 5;int y = 5;}mixin Foo;int y = 3;void main(){ writeln("x = ", x);// prints 3 writeln("y = ", y);// prints 3}
A mixin has its own scope, even if a declaration is overridden by the enclosing one:
import std.stdio : writeln;int x = 4;mixintemplate Foo(){int x = 5;int bar() {return x; }}mixin Foo;void main(){ writeln("x = ", x);// prints 4 writeln("bar() = ", bar());// prints 5}
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:
import std.stdio : writeln;mixintemplate Foo(){int x = 5;void func(int x) { }}mixintemplate Bar(){int x = 4;void func(long x) { }}mixin Foo;mixin Bar;void main(){ writeln("x = ", x);// error, x is ambiguous func(1);// error, func is ambiguous}
The call tofunc() is ambiguous becauseFoo.func andBar.func are in different scopes.
If a mixin has anIdentifier, it can be used to disambiguate between conflicting symbols:
import std.stdio : writeln;int x = 6;mixintemplate Foo(){int x = 5;int y = 7;void func() { }}mixintemplate Bar(){int x = 4;void func() { }}mixin Foo F;mixin Bar B;void main(){ writeln("y = ", y);// prints 7 writeln("x = ", x);// prints 6 writeln("F.x = ", F.x);// prints 5 writeln("B.x = ", B.x);// prints 4 F.func();// calls Foo.func B.func();// calls Bar.func}
Alias declarations can be used to form anoverload set of functions declared in different mixins:
mixintemplate Foo(){void func(int x) { }}mixintemplate Bar(){void func(long x) { }}mixin Foo!() F;mixin Bar!() B;alias func = F.func;alias func = B.func;void main(){ func(1);// calls B.func func(1L);// calls F.func}
Mixins can add virtual functions to a class:
import std.stdio : writeln;mixintemplate Foo(){void func() { writeln("Foo.func()"); }}class Bar{mixin Foo;}class Code : Bar{overridevoid func() { writeln("Code.func()"); }}void main(){ Bar b =new Bar(); b.func();// calls Foo.func() b =new Code(); b.func();// calls Code.func()}
An aggregate type can mixin additional destructors. Destructors are run in the opposite order to declaration order.
import std.stdio;mixintemplate addNewDtor(){ ~this() { writeln("Mixin dtor"); }}struct S{ ~this() { writeln("Struct dtor"); }mixin addNewDtor;}void main(){ S s;// prints `Mixin dtor`// prints `Struct dtor`}