Cast delegate and functions.
qznc
qznc at web.de
Thu Oct 31 06:21:21 PDT 2013
On Thursday, 31 October 2013 at 13:12:31 UTC, Wolftein wrote:
> void delegate(Event)
> void delegate(T) Where T is a class that inherits Event.
>
> I'm trying to cast (void delegate(T)) to (void delegate(Event))
> to be able to store them in a map, but when i cast them i get
> null exeception.
>
> Same thing for cast things like this
>
> TemplateClass!Plugin
> TemplateClass!OtherTypeOfPlugin -> Being OtherTypeOfPlugin
> inherit Plugin
>
> Using C++ this is allowed.
It is not safe to allow this.
auto f = delegate(T x) { x.methodOfT(); }
auto g = cast(void delegate(Event)) f;
g(new Event());
This would call methodOfT on an Event object, which does not have
that method.
Co- and Contravariance for such stuff is tricky. Here is a
discussion on how C# 4.0 approaches it:
http://stackoverflow.com/questions/245607/how-is-generic-covariance-contra-variance-implemented-in-c-sharp-4-0
More information about the Digitalmars-d-learn
mailing list