[racket] #%top and hopeless toplevel stuff

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Wed Aug 25 20:11:12 EDT 2010

At Wed, 25 Aug 2010 19:52:17 -0400, Danny Yoo wrote:
> 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

That's a syntax error.

> or the message:
>     set!: cannot set undefined variable: x

That's a run-time error.

> I'm trying to understand what's happening.  How does set! know in what
> context (definitions or interactions) it is being used?

The `module', `#%top', and `set!' forms of `racket/base' conspire. The
`#%top' and `set!' forms know when they are being expanded within a
`module', and they raise a syntax error in that case.

> What in the
> documentation should I be looking at to better understand this?

Maybe `#%top' or `set!', but the documentation could be clearer. For
example, whenever the documentation specifies an exception (as it does
for `set!' on an undefined top-level variable), it means that the
exception is at run time, but that probably isn't clear enough.

Posted on the users mailing list.