[racket] Attaching callbacks at runtime?

From: Matt Jadud (matt at jadud.com)
Date: Sat Jan 4 18:36:48 EST 2014

So obvious. Why I never thought of it, I don't know.

Clearly, I was stuck on wondering why there was no setter/getter for the
callback field. Just one level of indirection...

Cheers,
Matt


On Sat, Jan 4, 2014 at 5:55 PM, Matthias Felleisen <matthias at ccs.neu.edu>wrote:

>
> It is trivial to switch the callback's behavior at run-time:
>
> #lang racket/gui
>
> (define f (new frame% [label "test for Matt"]))
> (define b (new button%
>                [label "hello world"]
>                [parent f]
>                [callback (lambda (b e) (indirect-callback b e))]))
>
> (define (cb1 b e)
>   (displayln "odd")
>   (set! indirect-callback (if (eq? indirect-callback cb1) cb2 cb1)))
>
> (define (cb2 b e)
>   (displayln "even")
>   (set! indirect-callback (if (eq? indirect-callback cb1) cb2 cb1)))
>
> (define indirect-callback cb1)
>
> (send f show #t)
>
>
> On Jan 4, 2014, at 5:49 PM, Matt Jadud wrote:
>
> > Hi all,
> >
> > I've wondered about this for a while, but never asked.
> >
> > When I create a GUI widget, I attach a callback:
> >
> > (define b (new button% [... ...] [callback (lambda (b e) ...)] ...))
> >
> > Is it possible to attach a callback at runtime? The docs say "no," but I
> thought I'd ask regardless. Perhaps what I want to ask is "why?"
> >
> > I can't remember, at this point, why I thought I needed this
> functionality... but, I've always wondered why I wasn't able to do this.
> (Perhaps I was wondering this as I was thinking about a Qt-ish
> slot/connection model... I don't know.)
> >
> > Many thanks,
> > Matt
> >
> > ____________________
> >  Racket Users list:
> >  http://lists.racket-lang.org/users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140104/577c5271/attachment.html>

Posted on the users mailing list.