, 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, , .