Can someone explain how mixin is implemented?

Jacob Carlborg doob at me.com
Wed May 18 11:32:38 PDT 2011


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.

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list