[racket] Attaching callbacks at runtime?

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Sat Jan 4 17:55:09 EST 2014

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



Posted on the users mailing list.