Question about Mixin.

Agustin wolftein1 at gmail.com
Wed Jun 19 17:20:34 PDT 2013


On Wednesday, 19 June 2013 at 23:35:16 UTC, 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.
>
> Ali

Thanks!, now i'm trying to do that but its not working :(.

template Eventable(T) {
     final static __gshared public HandlerList!T getHandler() {
         if( handler_ is null ) {
             handler_ = new HandlerList!T();
         }
         return handler_;
     }
}

public class EventTemplate(T) : Event {
     mixin Eventable!T;
}

class TestEvent : EventTemplate!(TestEvent) {
     double x = 0.0;
}

TestEvent.getHandler() -> wont work.


More information about the Digitalmars-d-learn mailing list