On Monday, 5 November 2012 at 19:07:13 UTC, Robert wrote:
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.
I am embarrassed a little that a member function of the structure
looks like a static function,maybe it would be better if connect
took struct pointer directly.I think if struct become immortal it
will not be a big trouble,in other
case we have disconnect,that will help us.
Sorry for my awful english