Signals and Slots in D

Walter Bright newshound at digitalmars.com
Fri Sep 29 02:05:31 PDT 2006


Fredrik Olsson wrote:
> Walter Bright skrev:
>> Fredrik Olsson wrote:
>>> I like what I see. But there is a problem, a signal is hereby 
>>> identified by it's types only. In a real world scenario many signals 
>>> will have the same types. Bith a keyUp and a keyDown signal will 
>>> probably want to send  a key code of the same type.
>>
>> I don't understand the problem.
> Lets say you have a UI control that can emit two signals; Click and 
> DoubleClick, both send the mouse button as argument.
> 
> enum MouseButton { LEFT = 0, RIGHT = 1, MIDDLE = 3 };
> 
> class MyControl {
>   mixin Signal!(MouseButton);
> 
>   void myActualClick(MouseButton mb) {
>     ...
>     emit(mb);
>   }
> 
>   void myActualDoubleClick(MouseButton mb) {
>     ...
>     emit(mb);
>   }
> 
> }
> 
> 
> For the signal targets it will be impossible to tell a click from a 
> double click. Unless you pass a more arguments, but then you kind of 
> loose the simple idea of connecting to listen to a single event signal.

Ok, I see. The solution is straightforward:

class MyControl {
    mixin Signal!(MouseButton) Click;
    mixin Signal!(MouseButton) DoubleClick;

    void myActualClick(MouseButton mb) {
      ...
      Click.emit(mb);
    }

    void myActualDoubleClick(MouseButton mb) {
      ...
      DoubleClick.emit(mb);
    }
  }

You can have any number of signals in a class.



More information about the Digitalmars-d mailing list