[racket] Making namespaces meet

From: Ryan Culpepper (ryanc at ccs.neu.edu)
Date: Fri Nov 5 12:04:25 EDT 2010

On 11/05/2010 05:53 AM, Jukka Tuominen wrote:
>
> Hi,
>
> I'm having trouble of getting namespaces meet when making dynamic
> definitions. I made a simple example below.
>
> ((lambda ()
>     (define x 10)
>     (eval (list 'define 'y 5))
>     (+ x y)))
>
> ;>>  reference to undefined identifier: y
>
>
>
> I then tried to "store" one of the namespaces and pass it on to another, but
> I guess that's not the way the namespaces work.
>
>
>
> ((lambda ()
>     (define namespace (current-namespace))
>     (define x 10)
>     (eval (list 'define 'y 5) namespace)
>     (+ x y)))
>
> ;>>  reference to undefined identifier: y
>
>
> Any ideas how to solve this?

Both of your examples work at the racket repl. They should also work at 
the DrRacket repl. They fail only when you put them inside of a module 
(eg, the DrRacket definitions window after #lang racket), because a 
module must have no free references when it is compiled, even if those 
references would be defined during the execution of the module.

Read reflection chapter of the Guide for more information:

   http://docs.racket-lang.org/guide/reflection.html

The short answer is that to make DrRacket's definitions window act more 
like an ordinary repl, use the racket/load language. That is, change 
#lang racket to #lang racket/load. It seems you'll have to print out the 
results yourself, though; racket/load doesn't do it automatically.

Ryan


Posted on the users mailing list.