std.signals2 proposal

Robert jfanatiker at gmx.at
Mon Nov 5 11:08:38 PST 2012


> Hi!Could you write some examples for struct and non-object 
> delegates?
> 

Sure!

Something like:

struct Observer {
	void observe(int a, int b) {
		// ...
	}
}

void main() {
	Signal!(int, int) s1;
	Signal!int s2
	Observer o;
	s1.connect!Object(null, (null_object, a, b) => o.observe(a, b));
	s2.connect!Object(null, (null_object, a) => o.observe(7, a));

}

Having the delegate accept a null parameter might not be pretty, but I
consider this a good thing, because of the changed semantics: The signal
will keep a reference to the struct now, so the signals weak reference
semantics are no longer in place. (If struct is allocated on the heap,
it won't be freed as long as the signal is alive.)
But it is possible and safe. And if you know what you are doing also
very reasonable. 

But the main benefit is not that you can connect to structs (which is a
side effect), but that you can use wrapping delegates which do parameter
adoptions. That's the killer feature that proved to be so indispensable
and neat for me and others.

If really required it would not be to hard to provide an overload of
connect() which takes a struct pointer directly, just like the one
taking an object, but because of the changed semantics and the rare uses
I'd expect, probably not worthwhile. But comments are appreciated.





More information about the Digitalmars-d mailing list