Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

signal/slot like mechanism

6 views
Skip to first unread message

Yannick Le Saint

unread,
Mar 6, 2003, 1:27:15 PM3/6/03
to perl6-l...@perl.org

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 ?

Or is it maybe better if implemented in some perl6 module (don't think so)
??

Or maybe this matter has already been discussed and is now closed ???

And no, i'm not planning on doing it myself :-/

--
One good thing about music,
Well, it helps you feel no pain.
So hit me with music;
Hit me with music now.
-- Bob Marley, "Trenchtown Rock"

Luke Palmer

unread,
Mar 7, 2003, 6:34:43 AM3/7/03
to y.le...@free.fr, perl6-l...@perl.org
> 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!

> And no, i'm not planning on doing it myself :-/

Planning on changing your plans now?

Luke

Dan Sugalski

unread,
Mar 7, 2003, 12:50:30 PM3/7/03
to Yannick Le Saint, perl6-l...@perl.org
At 6:27 PM +0000 3/6/03, Yannick Le Saint 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 ?

I'm not exactly sure what you're looking for, since I'm not too
familiar with qt or glib, but if you mean will there be a mechanism
to register watcher subs that get called when things happen
internally (such as when a method is defined or redefined, or a class
changes, or something of the sort) then yes, we'll be doing that.

It solves a fairly thorny problem with making efficient specific
solutions in the face of a general problem, so there'll be built-in
facilities for it. (What it means for parrot, specifically, is that
we can build call structures and code designed specifically for
whatever is the current set of methods and class behaviours, knowing
that if something changes we can just rebuild our stuff based on the
new layout because we've got a notification method registered)
--
Dan

--------------------------------------"it's like this"-------------------
Dan Sugalski even samurai
d...@sidhe.org have teddy bears and even
teddy bears get drunk

Yannick Le Saint

unread,
Mar 7, 2003, 9:38:26 PM3/7/03
to fibo...@babylonia.flatirons.org, d...@sidhe.org, perl6-l...@perl.org

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.

0 new messages