mixin bug?

sldkf via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Aug 12 08:35:50 PDT 2016


On Friday, 12 August 2016 at 02:09:21 UTC, Engine Machine wrote:
> On Thursday, 11 August 2016 at 21:25:20 UTC, sldkf wrote:
>> On Thursday, 11 August 2016 at 20:27:01 UTC, Engine Machine

issue solved using a "template this parameter":

°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
template Cow()
{
     void soundImpl() { writeln("moo"); }
}

template Cat()
{
     mixin AnimalSound;
     void soundImpl() { writeln("meaow"); }
}

template Duck()
{
     mixin Cat;
     void soundImpl() { writeln("quack"); }
}

template AnimalSound()
{
     void emittSound(this T)() { (cast(T) this).soundImpl(); }
     // would also work with "this.soundImpl()"
}

struct Animal
{
     mixin Duck;
}

void main()
{
     Animal a;
     a.emittSound;
}
°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°

https://dlang.org/spec/template.html#TemplateThisParameter


More information about the Digitalmars-d-learn mailing list