mixin bug?

Engine Machine via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Aug 12 16:14:23 PDT 2016


On Friday, 12 August 2016 at 15:35:50 UTC, sldkf wrote:
> 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

This is not the solution to the original problem.

template X(T)
{
   int _len = 0;
   int Length() { return _len; }
   int length(this T)() { return (cast(T)this).Length(); }
}

template Y(T)
{
   mixin X!T;
   int Length() { return 3; }
}


Then calling length returns 0, while calling Length returns 3. 
But we obviously want length to "follow" Length, yet it doesn't 
due to D resolving length in X before it is mixed in Y!T.




More information about the Digitalmars-d-learn mailing list