[Issue 4000] New: Function pointer/delegate covariance

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Mar 22 07:11:15 PDT 2010


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

           Summary: Function pointer/delegate covariance
           Product: D
           Version: future
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: rejects-valid
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2010-03-22 07:11:14 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list