Can someone explain how mixin is implemented?

Jacob Carlborg doob at me.com
Thu May 19 11:35:56 PDT 2011


On 2011-05-19 15:43, Matthew Ong wrote:
> On 5/19/2011 2:32 AM, Jacob Carlborg wrote:
>>
>> Template mixins are not exactly like copy and paste code.
>>
>> * You can mixin the same template, at the same location, twice, taking
>> different parameters
>>
>> * Mixed in methods doesn't overload on existing methods in the scope
>> where the mixin is used
>>
>> I think all this is due to mixins being mixed-in in it's own scope.
>>
> Hi Jacob,
>
> The last message confused me. Please clarify.
>  >not exactly like copy and paste code.

I can try to example by giving an example:

template Foo (int i) {
     void foo () {}
}

class Bar {
     mixin Foo!(3);
     mixin Foo!(4);
}

The above code works and the two methods "foo" will not conflict. You 
can refer to them via the template:

Foo!(3).foo;

Second example:

template Foo () {
     void foo (int i) {}
}

class Bar {
     mixin Foo;
     void foo () {}
}

If template mixins where like copy and paste the first example would 
result in an error. In the second example you would have two methods 
named "foo" overloading each other. But that's not what's happening.


> Ok...So, does it meant that the compiler share some sort of binary?
>
> In java using template, the same LinkedList binary is shared for both
> String class type and also Integer class type.
>
> LinkedList<String> list=new LinkedList<String>();
> LinkedList<Integer> list=new LinkedList<Integer>();
>
> // Can also apply for Account.
> LinkedList<Account> list=new LinkedList<Account>();
>
>
> But there is a single LinkedList Class File(single binary).
>
> mixin template does seemed to be better than #define (uncheck by
> compiler directly) it can be any text. M4 is a tool exactly that purpose.
>
>
>


-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list