On Wed, 18 May 2011 09:45:56 -0400, Matthew Ong <[email protected]> wrote:

Hi,
From what I can see mixin in D is used in place of #define in C++(cool!!!). However, I do have a few question.

mixin with template does address some of this issue I supposed. That does allow me to define up to level of content of a class but is not class itself.

mixin template AType(T){ // but does not seems to allow me to inherit ClassC this level.
private:
    T value;
public:
    this(){...}
    void print(){...}
}

class ClassB : ClassC{ // ClassC Inheritance/Interface must only be done at this level?
    mixin AType!(string); // content
}

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?

There is a huge difference between #define and mixin. #define is a preprocessed macro. That means, before the compiler ever sees anything, the preprocessor blindly substitutes all the #defined symbols with their definitions. This means, you can put any text (even not-grammar-correct code) into a #define

A mixin is parsed by the compiler, which means it must follow the grammar of the language. You can't arbitrarily put mixins anywhere, they must be in places where mixins can be parsed.

I don't think you can mixin a base class, 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.

For instance (untested):

string inheritFromC(string classname, string classbodytemp)
{
  return "class " ~ classname ~ " : ClassC { mixin " ~ classbodytemp ~ ;}";
}

mixin(inheritFromC("ClassB", "AType!string"));

Note, I am *not* a mixin guru, so this might be totally bunk! But I think it works.

-Steve

Reply via email to