Suggestion: signal/slot mechanism

Juan Jose Comellas jcomellas at gmail.com
Sun Sep 3 08:47:30 PDT 2006


Bruno Medeiros wrote:
> The Signal and slots pattern is little more than an abstraction for
> languages that do not support delegates (and dynamic arrays). Which is
> not the case for D:
> 
>    // Declare a signal:
>    void delegate(Button, int)[] someSignal;
> 
>    // Connect a slot to a signal:
>    someSignal ~= foo.someSlot;
> 
>    // emit the signal
>    foreach(dg; someSignal)
>      dg(myButton, myInt);
> 
> The only limitation I see with D so far, is on the emit part. You can't
> create an emit function that works like this:
>    emit(someSignal, myButton, myInt);
> and that would do the same as that foreach. Because you cannot do
> "parameterized"(whether compile time or runtime) function calls.
> 

This problem is usually solved by creating a template signal class per
number of parameters that it can allow (i.e. the arity of the signal).
You'd have something like:

template Signal0() { ... }
template Signal1(T1) { ... }
template Signal2(T1, T2) { ... }
template Signal3(T1, T2, T3) { ... }
...

This is the solution used by libsigc++, boost, Qt Jambi, etc.





More information about the Digitalmars-d mailing list