Can someone explain how mixin is implemented?

Matthew Ong ongbp at
Thu May 19 06:36:45 PDT 2011

On 5/19/2011 9:18 PM, Matthew Ong wrote:
> On 5/19/2011 1:23 AM, Jesse Phillips wrote:
>> Matthew Ong Wrote:
>>> Perhaps I am missing something here. How can class level definition be
>>> part of the mixin?
>>> 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??
>> I can see what you're trying for and it looks like you'll need string
>> mixins to make it happen.
>> Mixing is similar to #define in that it does string substitution, but
>> it is unlike #define in that valid D is required declaration and call.
>> You'll have to modify this with string mixins (I'm surprised it
>> compiles excluding the non-existence of ClassB):
>> mixin template AType(alias T, U, alias V){class T : ClassC {
>> private:
>> U value;
>> public:
>> this(){}
>> void print(){}
>> mixin V;
>> }
>> }
>> class ClassC {}
>> mixin template btype() {
>> void someFunction() {};
>> }
>> mixin AType!("ClassB", string, btype);
>> void main() {
>> ClassC r = new ClassB();
>> }
> Hi Jesse,
> That is cool. Useful example like this should be within the
> documentation also. That helped me a lot. Now, I do not have to be
> concerned that the class level mixin usage class missed out the
> inheritance and also the implementation.
> This pattern is very useful for flattening the Object inheritance tree.
> D is cool!!!
Hi jesse,

I obtain error from: ClassC r = new ClassB();

src\main.d(27): Error: undefined identifier ClassB, did you mean class 
src\main.d(27): Error: ClassB is used as a type

Somehow this information is not known to the compiler....
mixin AType!("ClassB", string, btype);

Please help.

Matthew Ong
email: ongbp at

More information about the Digitalmars-d-learn mailing list