digitalmars.D - const "override" and interfaces
I've tried to implement a const, non-const accessor for a class similar to c++:
a& at(int) const;
a at(i);
With class it works as expected
class C1
{
int a;
const int foo() { return 2; }
ref int foo() { return a; }
}
Than I wanted to do it using interfaces:
interface I1 {
const int foo();
ref int foo();
}
class C2 : I1
{
int a;
const int foo() { return a; }
ref int foo() { return 2; }
}
but it won't compile (the interface itself compiles but i could not write any
class implementing the interface I1)
When inheriting interfaces do code won't even compile:
interface IConst {
const int foo();
}
interface IMutable : IConst {
ref int foo();
}
The compiler error i get (using dmd 2.032 on windows):
t2.d(71): Error: function t.IMutable.foo of type ref int() overrides but is not
covariant with t.IConst.foo of type const int()
t2.d(90): Error: function t.C2.foo of type ref int() overrides but is not
covariant with t.I1.foo of type const int()
Is it a normal behavior, a bug or i've missed something ?
Thanks
Oct 09 2009
It has some bug for sure, or the interface should be reconsidered:
interface I1 { int foo(); }
interface I2 { const int foo(); }
class C1 : I2,I1{
int foo() { return 0; }
const int foo() { return 1; }
}
class C2 : I1,I2{
int foo() { return 0; }
const int foo() { return 1; }
}
C1 won't compile, while C2 compiles.
For me it is quite strange why are the 2 cases distinct. The order of
inheritance should be identical and if the interfaces defines the same
members(functions), than only the same implementation should apply for both
interfaces.ex:
interface A { void foo() }
interface B { void foo() }
class C { void foo() {...} // implementation of both interfaces }
(I think in Java it works this way)
Tanks,
Oct 09 2009








gzp <galap freemail.hu>