The following code does not compile.
import std.stdio;
interface A {
public void a(int l);
}
class ACl:A {
public void a(int l) {
writeln("Hello a");
}
}
interface B: public A {
public void a(string l, int k);
}
class BCl: ACl, B {
public void a(string l, int k) {
writeln("Hello B.a", l, k);
}
}
int main() {
B b = new BCl();
b.a(1);
return 0;
}
However changing the second line of main() to (cast(A)b).a(1) makes it work.
Comment #1 by andrej.mitrovich — 2010-12-19T10:17:49Z
If I'm not mistaked:
B b = new BCl();
means the object b has a static type B, and the dynamic type BCl. Which means you can only call methods defined by the B interface. This is probably discussed more in TDPL, but I'd have to check it again because I'm not sure..
Comment #2 by andrej.mitrovich — 2010-12-19T10:18:32Z
Oh I see the problem now, B inherits from A, I missed that line.
Comment #3 by andrej.mitrovich — 2010-12-19T10:28:32Z
Yeah the only way this compiles is if you use the A type:
A b = new BCl();
b.a(1);
I don't see why using a B object wouldn't work. Bug, i guess..
Comment #4 by k.hara.pg — 2015-07-06T05:14:00Z
This is not a bug, it's normal name lookup behavior with inheritance. For example:
class A {
void foo(int l) {}
}
class B : A {
void foo(string l, int k) {}
}
void main() {
B b = new B();
//b.foo(0); // NG, because B.foo hides A.foo
b.foo("x", 1); // OK
}
There's two options:
1. Explicitly specify the base class/interface name on the foo call.
b.A.foo(1); // OK
2. Add an alias due to insert overload in the B.foo
class B : A {
alias foo = A.foo; // OK
void foo(string l, int k) {}
}
b.foo("x", 1); // ok
b.foo(1); // also OK