[plt-scheme] Reference to an identifier before its definition

From: Chongkai Zhu (czhu at cs.utah.edu)
Date: Wed Apr 14 08:21:45 EDT 2010

It all depends on when the reference is actually used. For your example, 
the (callback handle-ok) part is needed at the time the "(new button% 
..." is defined, while the "(send msg ..." thing is inside a lambda 
(although inexplicit in your code), which means the name is only used 
when the "handle-ok" function is called, at which time the msg is 
already defined. In general, variable inside a lambda can refer to a 
identifier that is defined later as long as the function is called after 
the identifier is defined.

Chongkai


Ferreira Maurizio wrote:
> As I try to start the following program, I receive the message
>
>   reference to an identifier before its definition: handle-ok
>
> I see that handle-ok is defined after its use, and if I move the definition before its use,
> (e.g at the top of the program), it works.
>
> However, in the definition of handle-ok, I CAN refer to msg, even if it is defined after its use.
>
> So the question is :
>
> Are forward references to definitions allowed ?
>
> Regards
> Maurizio
>
>
> ---- This doesn't works --------------
>
> #lang scheme/gui
>
> (define frame (new frame% [label "Example"]))
>
> (define msg (new message% [parent frame]
>                  [label "No events so far..."]))
>
> (new button% [parent frame]
>      [label "Click Me"]
>      (callback handle-ok))    ;  <--- ERROR
>
> (define (handle-ok button event)
>                  (send msg set-label "Button click"))
>
> (send frame show #t)
>
> ---- This works --------------
>
> #lang scheme/gui
>
> (define (handle-ok button event)
>                  (send msg set-label "Button click"))  ; <----  REFERENCE TO MSG IS OK !!!!!!
>
> (define frame (new frame% [label "Example"]))
>
> (define msg (new message% [parent frame]
>                  [label "No events so far..."]))
>
> (new button% [parent frame]
>      [label "Click Me"]
>      (callback handle-ok))
>
>
> (send frame show #t)
>   



Posted on the users mailing list.