Bug or Feature?
Sjoerd van Leent
svanleent at gmail.com
Tue Jun 13 12:59:55 PDT 2006
leoandru at gmail.com schreef:
> I started coding in D a few days now but I ran into a few problems, I wasn't
> sure if these were bugs so I didnt make a bug report. the problem is with
> interface inheritance and method signatures It doesn't ssem to work as expected
> and I don't see any documentation saying otherwise.
>
> interface A
> {
> void method();
> }
>
>
> interface B : A
> {
> void method(int a);
> }
>
>
> class Foo : B
> {
> }
>
>
> B ref = new Foo();
> ref.method(); //doesnt compile, requires an argument.
>
> This works if the method name in interface B is different from A, Im able to
> call method from interface. Am I doing something wrong? I expected this to work.
> I'm using dmd 0.160 compile on winxp. Thanks for any help/replies!
>
>
Well it isn't suppose to compile, this file, since the interface methods
need to be implemented anyway. Let's see the problem:
module test;
interface A {
void method();
}
interface B : A {
void method(int a);
}
class Foo : B {
}
void main() {
Foo foo = new Foo();
foo.method();
}
This generates the problem you where talking about. Let's first
implement the contract of interface A:
class Foo : B {
void method() {
int i = 5 + 8;
}
}
Now another error is going to pop up, which is yielding that the method
from interface B is not implemented. Exchange it with the contract of
interface B, not implementing interface A:
class Foo : B {
void method(int a) {
int i = 5 + a;
}
}
void main() {
Foo foo = new Foo();
foo.method(8);
}
And now we get again that function B.method is not implemented. This is
correct behaviour, since interfaces only know about contracts. So
interface B, by inheriting interface A makes function A.method also one
of the function B.method items.
The complete new source:
module test;
interface A {
void method();
}
interface B : A{
void method(int a);
}
class Foo : B {
void method() {
int i = 5 + 8;
}
void method(int a) {
int i = 5 + a;
}
}
void main() {
Foo foo = new Foo();
foo.method(8);
foo.method();
}
The compiler warning in the beginning has just to do with compile order.
Anyway, both methods should be implemented, so that's good. How the
compiler implements the order of testing I can't say, this is for Walter
to come up with.
Regards,
Sjoerd
More information about the Digitalmars-d
mailing list