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

From: Matthew Butterick (mb.list.acct at gmail.com)
Date: Tue May 14 12:09:02 EDT 2013

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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130514/c9a5de44/attachment.html>

Posted on the users mailing list.