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

MarisaLovesUsAll via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 21 14:07:22 PDT 2014


On Thursday, 21 August 2014 at 20:16:33 UTC, anonymous wrote:
> Maybe you can explain what you're trying to achieve with all
> this. There may be a different/better way to do it.

Sure.
Class tree: GameObject->Component->Sprite.
GameObject structure:
     Component[];
     Component addComponent(Component component);

First feature is Components Manager. It's an array and 
register()/remove() functions, just like in GameObject, but it's 
static and unique to all inherited classes.
     static T[] list;
     static void register(T obj);
     static void remove(T obj);
So, if I do this
     class Sprite:Component {};
'T' becomes 'Sprite', and Manager functionality adds to class.
It also will be able to create instance of class Component 
because of Components list in GameObject. And Component type must 
be one because it's easier to use one name for inheritance and 
for storing Components.
I did this by mixins, but it's not automatic.

And second feature. When I create Sprite and add it in 
GameObject, there will be no 'cast(Sprite)' constructions; type 
of return value must be argument's type.
Code:
Component addComponent(Component component);
Sprite shell = cast(Sprite) this.addComponent(new 
Sprite("shell_1.png")); //I want remove 'cast(Sprite)' from here

I found a solution for one class:
class Component
{
	Sprite toSprite() @property
	{
		return cast(Sprite) this;
	}
	alias toSprite this;
}
And it works. Now I want to add this solution in class Component, 
working with all inherited classes.
As I realize, this is rough and impossible, and I need to do some 
template magic in GameObject code.

Sorry for Engrish.
Regards, Alex


More information about the Digitalmars-d-learn mailing list