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

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 205 - Covariant return types don't work with forward-referenced types
Summary: Covariant return types don't work with forward-referenced types
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: x86 Windows
: P2 normal
Assignee: Walter Bright
URL:
Keywords: rejects-valid
Depends on:
Blocks:
Reported: 2006年06月18日 08:13 UTC by Hasan Aljudy
Modified: 2014年02月15日 13:20 UTC (History)
1 user (show)

See Also:


Attachments
Add an attachment (proposed patch, testcase, etc.)

Note You need to log in before you can comment on or make changes to this issue.
Description Hasan Aljudy 2006年06月18日 08:13:43 UTC
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 Stewart Gordon 2006年06月19日 10:32:13 UTC
(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 Walter Bright 2006年06月30日 20:31:19 UTC
Fixed DMD 0.162


AltStyle によって変換されたページ (->オリジナル) /