Is it ok to inherit multiple times same templated interface?
Alexandru Ermicioi via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Jan 15 12:33:30 PST 2017
Good day,
Given following code example, where a templated interface Wr, and
an implementation Im is present:
interface Wr(T) {
T get();
}
class Im(T : ubyte) : Wr!ubyte, Wr!ushort, Wr!string {
public T t;
ubyte get() {
return cast(ubyte) this.t;
}
ushort get() {
return cast(ushort) this.t;
}
string get() {
import std.conv;
return this.t.to!string ~ " with testings";
}
}
void main() {
auto i = new Im!ubyte;
i.t = 20;
assert((cast(Wr!ubyte) i).get == 20);
assert((cast(Wr!ushort) i).get == 20);
assert((cast(Wr!string) i).get == "20 with testings");
}
Is it ok (not undefined behavior), to have Im implementing
multiple times interface Wr, with different template arguments?
Or doing so, will eventually lead to subtle bugs?
Currently doing so is allowed, though, it is impossible to call
implemented methods directly from implementation.
Only by casting i to different implemented interfaces (Wr!ubyte,
Wr!ushort, and Wr!string), is possible to call each implemented
get method.
Thanks.
More information about the Digitalmars-d-learn
mailing list