Some help on Mixin Template at Class level.

Simen Kjaeraas simen.kjaras at gmail.com
Sat May 21 02:12:45 PDT 2011


On Sat, 21 May 2011 10:54:54 +0200, Matthew Ong <ongbp at yahoo.com> wrote:

> mixin template AType(alias T, U, alias V){
> class T : ClassC { // Class level Template

This gives you a class called T. You seem to want it to have the name
you pass as a string, in which case you have to use string mixins.

> private:
>      U value;
> public:
>      this(){}
>      void print(){}
>          mixin V;
> } // End Class
> }
>
> class ClassC {}
>
> mixin template btype() {
>      void someFunction() {}; // Content of a class.
>
> }
>
> mixin AType!("ClassB", string, btype);
>
> void main() {
>      ClassC r = new ClassB();
> }

As mentioned above, in order to use the name from the template parameter
you need to use string mixins. Here is how I would do it:

string ATypeGenerator( string name ) {
     return "class " ~ name ~ " : ClassC {
private:
     U value;
public:
     this(){}
     void print(){}
     mixin V;
}";
}

mixin template AType( string name, U, alias V ) {
     mixin( ATypeGenerator( name ) );
}

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

void main() {
     ClassC r = new ClassB();
}


-- 
Simen


More information about the Digitalmars-d-learn mailing list