Thread overview
Cast delegate and functions.
Oct 31, 2013
Wolftein
Oct 31, 2013
Wolftein
Oct 31, 2013
qznc
October 31, 2013
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
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
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