www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Delegate / functoin pointer flexibility

I'd like this to work:

int main()
{
    static class Foo { } // Class derived from Object

    void nf(Object o) { } // Nested function (delegate) taking an Object  
instead of Foo

    void delegate(Foo) dg = &nf; // test.d(10): cannot implicitly convert  
expression (&nf) of type void delegate(Object o) to void delegate(Foo)

    return 0;
}

where the parameters are at least base class types. This will let my event  
library handle derived classes better. e.g. code that knows about the more  
derived class won't need to cast the parameter, and code that doesn't can  
stick with the base class.


Return covariance could also be added:

int main()
{
    static class Foo { } // Class derived from Object

    Foo nf() { return new Foo; } // Nested function (delegate) returning  
Foo instead of Object

    Object delegate() dg = &nf; // test.d(10): cannot implicitly convert  
expression (&nf) of type Foo delegate() to Object delegate()

    return 0;
}
Mar 29 2006