Can someone explain how mixin is implemented?

Matthew Ong ongbp at yahoo.com
Thu May 19 06:43:14 PDT 2011


On 5/19/2011 2:32 AM, Jacob Carlborg wrote:
> On 2011-05-18 20:05, Jonathan M Davis wrote:
>>> On 5/18/2011 10:46 PM, Steven Schveighoffer wrote:
>>>> but you can do a string mixin to define the entire class. This is not
>>>> an easy thing to do, because you'd have to write the entire class as a
>>>> string of text.
>>>
>>> Thanks for the attempt and sample code.
>>>
>>> Seen that script import somewhere before. Perhaps others would like to
>>> help.
>>>
>>> Does mixin generate the same binary code as #define as inline code,which
>>> meant that same binary is repeated everywhere that macro is used?
>>>
>>> Or does it make a linked to the in a centralized locations that allow
>>> binary sharing when it is the same typed T??
>>>
>>> Do you have any idea? because the results is in dll/exe I cannot really
>>> tell. But someone that knows how the compiler works should be able to.
>>
>> First off, template mixins and string mixins are different beasts. In one
>> case, you're mixing in a template. In the other, you're mixing in
>> strings of
>> code. Template mixins must be valid code even without being mixed in.
>> String
>> mixins have to result in valid code, but they're strings so they can hold
>> anything. Both types of mixins, however, have to be explicitly mixed
>> in (as
>> opposed to the textual replacement that #defines do). In the case of
>> template
>> mixins, you're essentially copying and pasting code. In the case of
>> string
>> mixins, you're creating strings that are the code, so it's pretty much
>> just
>> like if you typed the code in there yourself except that the compiler is
>> putting it in there for you instead, allowing you to use functions to
>> generate
>> code and save you from having to type it all. There is no linking
>> involved in
>> mixins. It's not shared. You're generating code and inserting it where
>> you
>> used the mixin statement.
>>
>> - Jonathan M Davis
>
> 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.
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.



-- 
Matthew Ong
email: ongbp at yahoo.com



More information about the Digitalmars-d-learn mailing list