Only one signal per object?
Justin Whear
justin at economicmodeling.com
Wed Oct 12 13:14:00 PDT 2011
Yes, the docs don't elaborate or include an example. So, for posterity's
sake here's a quick example:
----------------------------------------
import std.stdio,
std.signals;
void main()
{
auto foo = new Foo;
auto listener = new Listener;
foo.onClick.connect(&listener.clickHandler);
foo.onFoo.connect(&listener.fooHandler);
foo.doClick(10, 10);
foo.doFoo("a message");
}
class Foo
{
void doClick(int x, int y) { onClick.emit(x, y); }
void doFoo(string s) { onFoo.emit(s); }
mixin Signal!(int,int) onClick;
mixin Signal!(string) onFoo;
}
class Listener
{
void clickHandler(int x, int y)
{
writeln("Clicked at ", x, ",", y);
}
void fooHandler(string bar)
{
writeln("Got ", bar);
}
}
----------------------------------------
$ rdmd test.d
Clicked at 10,10
Got a message
Spacen Jasset wrote:
> On 11/10/2011 18:12, Justin Whear wrote:
>> From the docs:
>> "Different signals can be added to a class by naming the mixins."
>>
>> So I think something like this ought to work:
>> mixin Signal!(string) onBlah;
>> mixin Signal!(int, int) onClicketyClick;
>>
>>
>> Peter Ravinovich wrote:
>>
>>> Is there a way to have several signals per object?
>>>
>>> The example in std.signal seams to suggest that only one signal per
>>> object can be emmited. Is it possible to have several events launched as
>>> it's possible in .NET?
>>>
>>> For example, bind one object to onClick and another to onKeyUp.
>>>
>>> Thanks
>>
> I think this has come up before. It doesn't seem particularly intuitive.
> Perhaps it should be that the signals must be explicit to make things
> clear cut.
More information about the Digitalmars-d-learn
mailing list