Mix
Mixins means different things that express different programming languages. In D, one mixed from one
The method of the template declaration extracts an arbitrary statement collection and inserts them into the current context.
Template mixed:
Mixin template logo;
Mixin template flag mixed into the flag;
Mixin template flag! (template parameter list);
Mixin template flag! (template parameter list) mixed into the flag;
Mixed marker:
Marker
TemplateMixin:
Mixin TemplateIndifier;
Mixin TemplateIndifier Mixinidentifier;
Mixin TemplateIdentifier! (TemplateArgumentList);
Mixin TemplateIdentifier! (TemplateargumentList) MixinIdentifier;
MixinIdentifier:
Identifier
The template mix can appear in the list of declarations of modules, classes, structures, union, and statements.
Template logo is a
Template declaration. in case
The template declaration does not have a parameter, you can use it without
! (
Template parameter list
Mixed form.
Unlike template, the process body mixed by the template is calculated in the scope of the mixed, not where the template declaration is defined. This is equivalent to inserting the molded process body into the mixed position using shear and paste. This is useful for the 'model file' of the injected parameters, and it is also useful to create a template nested function, and it is impossible to have a nested function under normal circumstances.
Template foo ()
{
INT x = 5;
}
Mixin foo;
Struct Bar
{
Mixin foo;
}
Void test ()
{
Printf ("x =% d / n", x); // Print 5
{
Bar B;
INT x = 3;
Printf ("B.x =% D / N", B.X); // Print 5
Printf ("x =% d / n", x); // Print 3
{
Mixin foo;
Printf ("x =% d / n", x); // Print 5
X = 4;
Printf ("x =% d / n", x); // Print 4
}
Printf ("x =% d / n", x); // Print 3
}
Printf ("x =% d / n", x); // Print 5
}
Mix can be parametric:
Template foo (t)
{
T x = 5;
}
Mixin foo! (int); // Creating the type INT XT
Mixing can add virtual functions for the class:
Template foo ()
{
Void func () {printf ("foo.func () / n");}
}
Class Bar
{
Mixin foo;
}
Class Code: Bar
{
Void func () {Printf ("code.func () / n");}
}
Void test ()
{
BAR B = New bar ();
B.Func (); // Call foo.func ()
B = new code ();
B.func (); // Call code.func ()
}
Mixed into the place where they appear, rather than in the template declaration:
INT Y = 3;
Template foo ()
{
Int abc () {returno y;}
}
Void test ()
{
INT Y = 8; mixin foo; // uses local Y instead of global Y
ASSERT (abc () == 8);
}
Mixing can use alias parameters to parameterize symbols:
Template foo (Alias B)
{
INT ABC () {RETURN B;}
}
Void test ()
{
INT Y = 8;
Mixin foo! (y);
ASSERT (abc () == 8);
}
This example uses a generic Duff's device that is incorporated for any statement (here, that statement is bold). In generating a nested function, a commissioned volume is generated, they will be inlined by the compiler:
Template duffs_device (alias ID1, alias ID2, alias s)
{
Void duff_loop ()
{
IF (id1 { 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 () {Printf ("foo / n");} Void test () { INT i = 1; INT J = 11; Mixin Duffs_Device! (i, j, delegate {foo ();}); Duff_loop (); // Perform foo () 10 times } The declaration in the mixed action domain mixed was imported in the amphiphilic domain. If there is the same name in the scope of the mixed and its surrounding, the declaration in the surrounding scope will cover the declaration of the mixed: INT x = 3; Template foo () { INT x = 5; INT Y = 5; } Mixin foo; INT Y = 3; Void test () { Printf ("x =% d / n", x); // Print 3 Printf ("y =% d / n", y); // Print out 3 } If two different mixeds are placed in the same scope, and they define the declarations of the same name, there is ambiguous error: Template foo () {int x = 5;} template bar () {INT x = 4;} mixin foo; mixin bar; void test () {printf ("x =% d / n", x); // error , X-moisture two} If there is a mix Mixed into the flag, it can be used to eliminate ambiguity: INT x = 6; Template foo () { INT x = 5; INT Y = 7; } Template bar () { INT x = 4; } Mixin foo f; Mixin Bar B; Void test () { Printf ("y =% d / n", y); // Print 7Printf ("x =% D / N", X); // Print 6 Printf ("f.x =% d / n", f.x); // Print 5 Printf ("B.x =% D / N", B.X); // Print 4 } Mixed into its own scope, although the declaration will be covered by the periphery: INT x = 4; Template foo () { INT x = 5; Int bar () {returnx x;} } Mixin foo; Void test () { Printf ("x =% d / n", x); // Print 4 Printf ("BAR () =% D / N", BAR ()); // Print out 5 }