Question about Mixin.

Jonathan M Davis jmdavisProg at gmx.com
Wed Jun 19 16:43:56 PDT 2013


On Wednesday, June 19, 2013 16:35:16 Ali Çehreli wrote:
> 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.

Ah, you're right. That will work. I misread the question. I thought that he 
was asking whether mixins in the base class magically got mixed in again into 
the derived class, which they don't.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list