Can someone explain how mixin is implemented?

Jonathan M Davis jmdavisProg at gmx.com
Wed May 18 11:05:12 PDT 2011


> 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


More information about the Digitalmars-d-learn mailing list