Multiple inheritance and covariant return types.
Frank Fischer
frank.fischer at s2001.tu-chemnitz.de
Thu Aug 9 13:05:34 PDT 2007
Bruno Medeiros wrote:
> Frank Fischer wrote:
>> I have another question on multiple inheritance. The following code does
>> not work (tested with DMD 1.020 and 2.003)
>>
>> ---
>> interface A {}
>>
>> interface B {}
>>
>> interface AB: A, B {}
>>
>> abstract class X {
>> abstract A func();
>> }
>>
>> interface Y {
>> B func();
>> }
>>
>> class XY: X, Y {
>> AB func() { return null; }
>> }
>> ---
>> The compiler complains about the line 'AB func() { return null; }':
>>
>> t.d(38): function t.XY.func incompatible covariant types A() and B()
>>
>
>
> It's not a bug.
> Classes are *convertible* to interfaces that it implements but they are
> not *covariant* with said interfaces. The same thing happens for an
> interface and it's super interfaces. The interface is not covariant with
> is super interfaces, only convertible to them. (exception maybe the
> first interface). Thus in the example above AB is not both an A and B.
> This behavior is different than Java and other OO languages, and is so
> for performance reasons. There are some tricks the compiler does to
> allows override as if the types were covariant, but it only works in
> some situations.
Thanks for the explanations. So I have to live with that behaviour and
handle the problem in a different way.
Frank
More information about the Digitalmars-d-learn
mailing list