Bug 4000 – Function pointer/delegate covariance

Status
RESOLVED
Resolution
DUPLICATE
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-03-22T07:11:00Z
Last change time
2015-06-09T05:13:48Z
Keywords
rejects-valid
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2010-03-22T07:11:14Z
This page about C#: http://msdn.microsoft.com/en-us/library/ms173174%28VS.80%29.aspx contains this working C# code: class Mammals {} class Dogs : Mammals {} class Program { public delegate Mammals HandlerMethod(); public static Mammals FirstHandler() { return null; } public static Dogs SecondHandler() { return null; } static void Main() { HandlerMethod handler1 = FirstHandler; HandlerMethod handler2 = SecondHandler; } } This is a possible translation to D2: class Mammals {} class Dogs : Mammals {} alias Mammals function() HandlerMethod; Mammals function() handler; Mammals FirstHandler() { return null; } Dogs SecondHandler() { return null; } void main() { HandlerMethod handler1 = &FirstHandler; HandlerMethod handler2 = &SecondHandler; // line 9, err handler = &SecondHandler; // line 10, err } But the D2 code gives two compile errors: test.d(9): Error: cannot implicitly convert expression (& SecondHandler) of type Dogs function() to Mammals function() test.d(10): Error: cannot implicitly convert expression (& SecondHandler) of type Dogs function() to Mammals function() It can be positive for the D type system to accept this code too.
Comment #1 by yebblies — 2011-06-08T22:48:58Z
I'm going to mark this as a dupe of 3180, along with 3833, as both cases are covered by covariance. *** This issue has been marked as a duplicate of issue 3180 ***