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

MarisaLovesUsAll via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 21 05:58:12 PDT 2014


I found a rough solution. It's not ideal and I still want to make 
autoinject, but it works.

mixin template Manager(T) {};
class Component {};
class Sprite:Component
{
     mixin Manager!Sprite;
};


1) How to make mixin inject automatic?
2) If it's impossible, how to use "mixin Manager;" without 
"!Sprite" ?
"mixin template Manager(this T) {};" isn't working.

Regards, Alex

On Thursday, 21 August 2014 at 09:38:13 UTC, MarisaLovesUsAll 
wrote:
> 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