Thread overview |
---|
October 31, 2013 Cast delegate and functions. | ||||
---|---|---|---|---|
| ||||
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. |
October 31, 2013 Re: Cast delegate and functions. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Wolftein | 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.
|
October 31, 2013 Re: Cast delegate and functions. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Wolftein | 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 |
Copyright © 1999-2021 by the D Language Foundation