delegate/function ptr implicit casting
Steven Schveighoffer
schveiguy at yahoo.com
Mon Sep 20 04:46:56 PDT 2010
One of the huge limitations of D is delegate/function ptr implicit
casting. What I mean is, implicitly casting a delegate from one type to
another when the types of the arguments implicitly cast (without
modification).
An example of this is contravariance. For example:
class A {}
class B : A {}
void main()
{
int foo(A a) {return 1;}
int delegate(B b) x = &foo; // should work.
}
Another example is for constancy:
void foo(const(int)[] arr) {}
void function(int[] arr) x = &foo;
(note that this latter one works, but due to a compiler bug)
Finally, you should be able to implicitly cast away pure/nothrow.
pure nothrow int foo(int x) {return x;}
int function(int) x = &foo;
What I meant by "when the types of the arguments implicitly cast" was to
deal with the "implicit" casting of types. For instance, you can
implicitly cast an int to a long, but you could not create a delegate that
takes an int out of a function that takes a long. Same thing for
classes/interfaces.
Thoughts? Walter previously rejected the contravariance for delegates
idea on the grounds that he didn't want to do contravariance for
everything (I believe contravariance just for delegates/functions is a
perfectly encapsulated use).
Note that this would help in certain aspects of the runtime. For example,
see bug http://d.puremagic.com/issues/show_bug.cgi?id=3715
-Steve
More information about the Digitalmars-d
mailing list