Suggestion: signal/slot mechanism

Lutger lutger.blijdestijn at gmail.com
Thu Sep 7 03:34:27 PDT 2006


Kristian wrote:
<snip>
> 
> Well now, this is almost like having a direct support! :)
> 
> One have to use a mixin and the signal names cannot be overloaded. 
> However, these are not big drawbacks at all. Actually the overload thing 
> is a benefit when connecting: you don't have to define parameter types. 
> I like the syntax very much indeed; this is a must for Phobos...! ;)

Well thanks. I'll have to make some more tests before putting it online 
somewhere, then I'll see what it's worth.

> At first I thought that I would like the 'connect()' function to have 
> parameters swapped (e.g. "foo.valueChanged1.connect(this, 
> handleFoo());"), but now I think the current order is better.

It's more consistent with free functions / unmanaged slots:

sig.connect(&fooFunc);

// alternative syntax:
sig ~= { writefln ("hello world"); };

> BTW, I'm wondering why there is 'void' in "Signal!(void, int, int)"? Is 
> it intentional?
>

Yes, it's the return value of the signature. Normally a signal returns 
whatever the last slot returns, if it does. With foreach traversal, you 
can do something similar to combiners in boost::signals. See 
http://www.boost.org/doc/html/signals.html.

Here's an example:

// sum for Signal!(int, <any type>)
int sum(SIGT, ARGT) (SIGT signal, ARGT arg)
{
     int result;
     foreach(slot; signal)
         result += slot(arg);
     return result;
}
	







More information about the Digitalmars-d mailing list