View mode: basic / threaded / horizontal-split · Log in · Help
November 16, 2012
Delegate bug?
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.)
Top | Discussion index | About this forum | D home