Question about Mixin.

Ali Çehreli acehreli at yahoo.com
Wed Jun 19 16:35:16 PDT 2013


On 06/19/2013 04:29 PM, Agustin wrote:
> Hello guys, my question is, its possible to write a mixin in a class,
> then if that class is inherited, the mixin will be written again instead
> of written the mixin again in the class child, for example:
>
> Class A(T)
> {
>   mixin(WriteFunctionFor!(A));
> }
>
> Class B : A(B)
> {
>    ... -> mixin is written for B without need to write ("mixin(Write...))")
> }
>
> Class C : A(C)
> {
>    ... -> mixin is written for C without need to write ("mixin(Write...))")
> }

Yes:

import std.stdio;

template WriteFunctionFor(T)
{
     T data;

     void foo()
     {
         writefln("I am working with a %s.", T.stringof);
     }
}

class A(T)
{
     mixin WriteFunctionFor!T;
}

class B : A!B
{}

class C : A!C
{}

void main()
{
     auto b = new B();
     b.foo();

     auto c = new C();
     c.foo();
}

The output:

I am working with a B.
I am working with a C.

Ali



More information about the Digitalmars-d-learn mailing list