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

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 21 06:19:06 PDT 2014


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

You could use this pattern:

interface Component {}
class ComponentImpl(T) {}
class Sprite : ComponentImpl!Sprite {}

It's not 100% automatic, but the duplication of "Sprite" is in
the same line. And it's not as easy to forget as a mixin,
especially when Component does declare methods that ComponentImpl
implements.

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

You can use `typeof(this)` in Manager:

mixin template Manager()
{
      void someMethod(typeof(this) otherInstance) {}
}


More information about the Digitalmars-d-learn mailing list