Warning of excessive methods when using mixin

I would like to use mixin to implement the interface. This works fine until I subclass. The problem is that the mixin template also implements the template function.

Sort of:

interface Features {
    void feature1();
    void feature2();
}

mixin template FeaturesImplementer() {
    int bar;
    final void feature1(){}
    void feature2(){}
    void typeStuff(T)(){}
}

class WithFeature: Features {
    mixin FeaturesImplementer;
    this(){typeStuff!(typeof(this));}
}

class AlsoWithFeature: WithFeature {
    mixin FeaturesImplementer;
    this(){typeStuff!(typeof(this));}
}

void main() {
    new WithFeature;
    new AlsoWithFeature;
}

outputs:

Error: function AlsoWithFeature.FeaturesImplementer! (). feature1 cannot override the final function WithFeature.FeaturesImplementer! (). feature1

Deprecation: Implicit overriding of the base class method WithFeature.FeaturesImplementer! (). feature2 with AlsoWithFeature.FeaturesImplementer! (). feature2 debrecated; add override attribute

Error: mixin AlsoWithFeature.FeaturesImplementer! () Creating errors

typeStuff , , FeaturesImplementer . . mixin everuthing ?

+4
1

, static if, . , , , , hasMember.

std.traits, , , , ancestor (Object) Feature.

:

interface Features {
    void feature1();
    void feature2();
}

mixin template FeaturesImplementer() {

    import std.traits: BaseClassesTuple;
    alias C = typeof(this);
    enum OlderHave = is(BaseClassesTuple!C[0] : Features);
    enum Have = is(C : Features);
    enum Base = Have & (!OlderHave);

    static if (Base || !__traits(hasMember, C, "bar"))
    int bar;

    static if (Base || !__traits(hasMember, C, "feature1"))
    final void feature1(){}

    static if (Base || !__traits(hasMember, C, "feature2"))
    void feature2(){}

    void typeStuff(T)(){}
}

class WithFeature: Features {
    mixin FeaturesImplementer;
    this(){typeStuff!(typeof(this));}
}

class AlsoWithFeature: WithFeature {
    mixin FeaturesImplementer;
    this(){typeStuff!(typeof(this));}
}

void main() {
    new WithFeature;
    new AlsoWithFeature;
}

, .

, - , int , , , WithFeature AlsoWithFeature, , .

+1

Source: https://habr.com/ru/post/1616263/


All Articles