Bug 205 – Covariant return types don't work with forward-referenced types

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2006-06-18T08:13:00Z
Last change time
2014-02-15T13:20:38Z
Keywords
rejects-valid
Assigned to
bugzilla
Creator
hasan.aljudy

Comments

Comment #0 by hasan.aljudy — 2006-06-18T08:13:43Z
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>
Comment #1 by smjg — 2006-06-19T10:32:13Z
(In reply to comment #0) > 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.
Comment #2 by bugzilla — 2006-06-30T20:31:19Z
Fixed DMD 0.162