On Friday 07 March 2003 11:34, Luke Palmer wrote:
> > Hi guys, i was just wondering if some notification mechanism (
> > signal/slot alike ) was planned in perl 6, like they already are in
> > glib and qt ?
>
> class Signal {
> has @.dest;
>
> method emit($code) { $code($_) for @.dest }
> method attach($obj) { push @.dest: $obj }
> }
>
> class Foo {
> has Signal $.valueChanged is public;
> has $.value is public;
>
> method value($newval) { # Overriding default accessor
> $.value = $newval;
> emit $.valueChanged: { .($newval) };
> $.value
> }
> }
>
> my Foo $a, $b;
> $a.valueChanged.attach($b, { $b.value($_) });
>
>
> Or something like that. Already supported :)
>
> It's neat how powerful the closure is. I can't wait until I
> understand continuations!
>
I was rather thinking about something lesser code intrusive :
class Senator {
has $.money is public;
has %.classifiedData is public;
method travel($country, $date) {
# do something suspect
}
}
class LicensedToKill {
has @.work is public;
method watchAccount($who) {
# may be normal though
}
method lookAtClassifiedData($vip,@dataKeys) {
# i'm a thief, remember ?
}
method makeContactIn($vip,$country) {
# try to make a new ally
}
}
my Senator $guessWho;
my LicensedToKill $jamesBond;
my @jamesBondWork = ();
# called every time $guessWho.money is changed
@jamesBond.work[0] =
spy(
$guessWho.money,
{ $jamesBond.watchAccount($guessWho) }
);
# called every time $guessWho.ClassifiedData is modified,
# means some key(s) are created, deleted or data has changed
# @_ should contain all concerned keys
@jamesBond.work[1] =
spy(
$guessWho.classifiedData,
{ $jamesBond.lookAtClassifiedData($guessWho,@_) }
);
# called every time something is calling $guessWho.travel(...)
# @_ contains travel(...) call arguments
@jamesBond.work[2] =
spy(
$guessWho.travel,
# i'll be waiting for him, do not need $date, just $country
{ $jamesBond.makeContactIn($guessWho,@_[0]}
);
# at some point, $guessWho joins the dark side,
# we do not need to monitor his classified data any more
splice @jamesBond.work,1,1;
This way spied class does not need to do anything special (appart from
providing public elements :) ), thus any class is signal-handling ready :p
Safety concerns :
- what happen when $jamesBond or $guessWho is deleted ??
-> all associated handlers are removed
(it may not be that easy to implement)
-> *and* spy()'s magical return value just becomes undef.
- if spy()'s magical return value is deleted or overwritten, then
its associated handler is removed.
Improvements :
- How could $jamesBond be notified when $guessWho is deleted ?
-> Still do not know ... :(
> > And no, i'm not planning on doing it myself :-/
>
> Planning on changing your plans now?
>
> Luke
It sure is prematurate for me now :(, but i would enjoy (using) perl later
:)
Hope it helps :)
Regards.
--
If you explain so clearly that nobody can misunderstand, somebody will.