www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 205] New: Covariant return types don't work properly

reply d-bugmail puremagic.com writes:
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 digitalmars.com
        ReportedBy: hasan.aljudy 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>


-- 
Jun 18 2006
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=205


smjg iname.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |smjg iname.com
            Summary|Covariant return types don't|Covariant return types don't
                   |work properly               |work with forward-referenced
                   |                            |types






 If your interface has a method whose return type is another interface,
 covariance won't work.
Actually, the failure of your code has nothing to do with the fact that they're interfaces. It's a case of a bug I've noticed before, whereby covariance fails if the return type is forward referenced. For a real instance of what you've written in your first sentence, see issue 210. --
Jun 19 2006
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=205


bugzilla digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED





Fixed DMD 0.162


-- 
Jun 30 2006