CT Inheritence structures

Jack Applegame via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Aug 20 02:42:08 PDT 2016


On Saturday, 20 August 2016 at 00:46:15 UTC, Engine Machine wrote:
> Any ideas?
Something like this?

mixin template TypeData(string type: "Animal") {
     int y;
}
mixin template TypeData(string type: "Dog") {
     int z;
}
mixin template TypeData(string type: "Pug") {
     int s;
}

template Type(string type, ARGS...) {
     static if(ARGS.length == 0) {
         class Type {
             mixin TypeData!type;
         }
     } else {
         class Type : Type!ARGS {
             mixin TypeData!type;
         }
     }
}

void main() {
     auto a = new Type!("Pug", "Dog", "Animal")();
     Type!("Dog", "Animal") b = a;	
     Type!("Animal") c = b;	
	
     a.s = 1;
     b.z = 2;
     c.y = 3;

     pragma(msg, typeof(a));
     pragma(msg, typeof(b));
     pragma(msg, typeof(c));
}

See result - https://dpaste.dzfl.pl/1a76490aaf55


More information about the Digitalmars-d-learn mailing list