[Issue 205] New: Covariant return types don't work properly

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Jun 18 06:13:44 PDT 2006


http://d.puremagic.com/issues/show_bug.cgi?id=205

           Summary: Covariant return types don't work properly
           Product: D
           Version: 0.160
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: hasan.aljudy at gmail.com


If your interface has a method whose return type is another interface,
covariance won't work.

sample code:
<code>
interface Iface1
{
    Iface2 func1();
}

interface Iface2
{
    Iface1 func2();
}

class C1 : Iface1
{
    C2 func1(){    return null; }
}

class C2 : Iface2
{
    C1 func2(){    return null; }
}

void main()
{
}
</code>

when building, I get:
main.d(13): function main.C1.func1 of type C2() overrides but is not covariant
with main.Iface1.func1 of type Iface2()

Here, interface Iface1 declares a method func1 of type Iface2. We have a class
C2 which implements Iface2, and a class C1 which implements Iface1. If we try
to implement func1 in C1 with a return type of C2, the compiler complains that
this return type is not covariant with Iface2, even though C2 does implement
Iface2.

if instead we implement Iface1 and Iface2 in one class, eveything works
properly. 
The following example produces no errors.
<code>
interface Iface1
{
    Iface2 func1();
}

interface Iface2
{
    Iface1 func2();
}

class C : Iface1, Iface2
{
    C func1(){ return null; }   
    C func2(){ return null; }
}

void main()
{
}
</code>


-- 




More information about the Digitalmars-d-bugs mailing list