www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Cast delegate and functions.

reply "Wolftein" <agustin.l.alvarez hotmail.com> writes:
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.
Oct 31 2013
next sibling parent "Wolftein" <agustin.l.alvarez hotmail.com> writes:
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.
Well with the last verion delegate and function cast works fine, but not for TemplateClass.
Oct 31 2013
prev sibling parent "qznc" <qznc web.de> writes:
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 http://stackoverflow.com/questions/245607/how-is-generic-covariance-contra-variance-implemented-in-c-sharp-4-0
Oct 31 2013