Delegate bug?

Stugol stugol at gmx.com
Thu Nov 15 19:51:20 PST 2012


Hi. I'm writing an Event class, and have the following code 
(important line highlighted):

    class Event(TEventArgs : EventArgs = EventArgs) {
       alias void delegate(Object, TEventArgs) THandlerFn;
       SList!(THandlerFn) handlers;

       Event opOpAssign(string op)(void delegate() handler) {
          static if (op == "+") {
>>>         handlers.insert( (Object o, EventArgs e) { handler(); 
>>> } );
             return this;
          } else static assert(0, "Operator "~op~" not 
implemented");
       }
    }

Usage:

    Event!EventArgs Close;
    Close += { ... some delegate code ... };

Calling the first (and only) delegate in the "handlers" list 
fails to call my delegate code.

If I change the function to the following (changes highlighted):

    Event opOpAssign(string op)(void delegate() handler) {
       static if (op == "+") {
>>>      auto wrapper = (Object o, EventArgs e) { handler(); };
>>>      handlers.insert(wrapper);
          return this;
       } else static assert(0, "Operator "~op~" not implemented");
    }

It works without any problem. Why is this?

(In case you're wondering, I'm wanting the ability to pass in a 
delegate accepting no arguments, in place of a delegate accepting 
an Object and an EventArgs.)


More information about the Digitalmars-d mailing list