Auto-add static field when inherit // mixins, templates?

MarisaLovesUsAll via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 21 02:38:11 PDT 2014


tl;dr - how to get child classname from inherited parent function 
at compile time?
class A { string getName(); };
class B { };
B foo = new B;
assert(foo.getName() == "B");
...

Hi! I'm stuck at one issue, and I don't know how to solve it. I 
think this is about mixins/templates, isn't it?
When inherit from base class Component, I need to auto-create 
child own static fields with child type.
It should look like this, after compilation:

class Component
{
     //it doesn't matter to have any fields here
     //but it's important to be able to create an instance of 
Component
     //and when inherit, all childs will get their own "static T 
list;" where T is a type of child.
};
class Sprite:Component
{
     static Sprite list; //auto-added
     static void fun() { } //auto-added, operates with Sprite
}
class Camera:Component
{
     static Camera list; //auto-added
     static void fun() { } //auto-added, operates with Camera 
instead of Sprite
}
...
//so this must be correct:
Component foo;
Sprite bar;
void foobar(Component one) { }
foobar(Sprite);
...

Sorry for bad English.
Best regards, Alex


More information about the Digitalmars-d-learn mailing list