www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Hiding mixin template members should be allowed.

reply Timon Gehr <timon.gehr gmx.ch> writes:
I don't know since when, but hiding mixin template members was apparently
deprecated.
What about this use case? This particular instance can be resolved easily, but
what if you want to implement multiple interfaces that all want to provide
overridable default implementations using a mixin template?

If this does not work anymore, are mixin templates any good? Is there a simple
workaround I am missing?

import std.stdio;
interface Duck{
    void quack();
    void walk();
}
mixin template DuckImpl(){
    void quack(){
        writeln("quack!");
    }
    void walk(){
        writeln("walking...");
    }
}
class NormalDuck: Duck{
    mixin DuckImpl;
}
class TalkyDuck: Duck{
    void quack(){ // does not work anymore

        writeln("quack! quack!");
    }
    mixin DuckImpl;
}


void main(){
    Duck a=new NormalDuck;
    Duck b=new TalkyDuck;
    a.quack(); a.walk();
    b.quack(); b.walk();
}

tt.d(20): Error: class tt.TalkyDuck use of tt.TalkyDuck.DuckImpl!().quack()
hidden by TalkyDuck is deprecated

Cheers,
-Timon
Aug 01 2011
parent Timon Gehr <timon.gehr gmx.ch> writes:
Oh, I see, the problem is that the functions in the mixin would call the hidden
functions instead of the new ones.

I have also found the workaround:

class DefaultTalkyDuck: Duck{
    mixin DuckImpl;
}
class TalkyDuck: DefaultTalkyDuck{
    override void quack(){writeln("quack! quack!");}
}

Sorry for the noise.

-Timon
Aug 01 2011