[racket] #%top and hopeless toplevel stuff

From: Danny Yoo (dyoo at cs.wpi.edu)
Date: Wed Aug 25 19:52:17 EDT 2010

I wanted to make sure I understand how toplevel variables, REPLs and
namespaces work.  I need to understand this because what I'm doing in
WeScheme's REPL is not quite right because it doesn't match what is
happening with DrRacket's REPL.

I see that the error messages regarding toplevel variables are
slightly different, depending on context.  e.g. set! can produce
either the error message

    set!: unbound identifier in module in: x

or the message:

    set!: cannot set undefined variable: x

depending on the compilation context.  Furthermore, from talking with
Carl Eastlund, I understand that interactions do something with
namespaces when hitting toplevel variables.  The following transcript
confirms to me that toplevel variable lookup and set! cooperates with
namespaces, at least at the REPL:

> my-unbound-variable
reference to undefined identifier: my-unbound-variable

> (set! x 16)
set!: cannot set undefined variable: x

> (namespace-set-variable-value! 'my-unbound-variable "hello world")

> my-unbound-variable
"hello world"

> (set! my-unbound-variable 'ok)

> my-unbound-variable

I'm trying to understand what's happening.  How does set! know in what
context (definitions or interactions) it is being used?  What in the
documentation should I be looking at to better understand this?

Posted on the users mailing list.