[racket] Question about resolution of variable-name conflicts in namespaces

From: Robby Findler (robby at eecs.northwestern.edu)
Date: Tue May 14 20:13:58 EDT 2013

The short answer is that the top-level is hopeless, as Matthew has
discussed at some length here and elsewhere.

If you really need to eval code, can you first put it into a module?

Robby


On Tue, May 14, 2013 at 11:09 AM, Matthew Butterick
<mb.list.acct at gmail.com>wrote:

> Suppose x-is-foo.rkt is this:
>
> (module x-is-foo racket
>   (define x 'foo)
>   (provide x))
>
> If you open another file and try this:
>
> (require "x-is-foo.rkt")
> (define x 'bar)
>
> You'lll get a "identifier already imported" error. OK, that much I
> understand.
>
> Here's the question. When you do this:
>
> (parameterize ([current-namespace (make-base-empty-namespace)])
>   (namespace-require 'racket)
>   (namespace-require "x-is-foo.rkt")
>   (namespace-set-variable-value! 'x 'bar)
>   (eval '(print x) (current-namespace)))
>
> This time, you get 'foo. Why 'foo? Why not another "identifier already
> imported" error?
>
> I assume I'm missing a subtlety of how the namespace environment is
> different. But according to the docs, both namespace-require and
> namespace-set-variable-value! affect the top-level environment of the
> namespace. So I don't see why the require is silently overriding the
> set-variable-value, rather than causing a conflict.
>
> It's not a sequencing issue, because if you swap the two lines:
>
> (parameterize ([current-namespace (make-base-empty-namespace)])
>   (namespace-require 'racket)
>   (namespace-set-variable-value! 'x 'bar)
>   (namespace-require "x-is-foo.rkt")
>   (eval '(print x) (current-namespace)))
>
> You still get 'foo.
>
> Only if you remove the require line:
>
> (parameterize ([current-namespace (make-base-empty-namespace)])
>   (namespace-require 'racket)
>   (namespace-set-variable-value! 'x 'bar)
>   (eval '(print x) (current-namespace)))
>
> Do you get 'bar.
>
>
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130514/3f570a6b/attachment.html>

Posted on the users mailing list.