[racket] Thought about namespaces

From: Eric Dobson (eric.n.dobson at gmail.com)
Date: Sun May 26 14:38:26 EDT 2013

Try using make-empty-namespace instead of make-base-empty-namespace.
It won't work but will explain why the add1 procedures are the same
when you do get it to work, with the change below.

(define-namespace-anchor anchor)

(define (get-from-fresh-namespace var)
 (let ((namespace (make-empty-namespace)))
  (parameterize ((current-namespace namespace))
    (namespace-attach-module (namespace-anchor->namespace anchor)
   (namespace-require 'racket)
   (namespace-variable-value var #t (λ () 'error)))))

You are using the the same builtin module instance in your example but
not the same racket instance. If you use the same racket instance then
you get the same answer for both values. If you could somehow get a
different '#%builtin module then the add1's might be different but I
don't think that is possible.

On Sun, May 26, 2013 at 10:43 AM, Jos Koot <jos.koot at gmail.com> wrote:
> #| Consider: |#
> #lang racket
> (define (get-from-fresh-namespace var)
>  (let ((namespace (make-base-empty-namespace)))
>   (parameterize ((current-namespace namespace))
>    (namespace-require 'racket)
>    (namespace-variable-value var #t (λ () 'error)))))
> (eq?
>  (get-from-fresh-namespace 'add1)
>  (get-from-fresh-namespace 'add1)) ; -> #t
> (eq?
>  (get-from-fresh-namespace 'force)
>  (get-from-fresh-namespace 'force)) ; -> #f
> #|
> It is clear to me why the last form produces #f. Procedure force is a
> predicate of a struct and is exported by module
> .../collects/racket/promise.rkt. For each fresh empty base-namespace the
> form (namespace-require 'racket) uses a distinct instance of this module.
> Each instance defines the promise-struct freshly and provides distinct
> variable- and syntax-bindings related to promises. Is my observation
> correct?
> It is little bit confusing that procedure get-from-fresh-namespace, when
> called with the same variable-name, in some cases returns identical values
> and in others does not.
> I think it is not easy to make Racket such as to make it procedure
> get-from-fresh-namespace always to return distinct objects (not eq?) or
> always to return identical objects (eq?) when called with the same
> variable-name.
> I know, I am comparing procedures, but as
> |# (let ((a add1)) (eq? a add1)) #|
> is guaranteed to return #t, I wonder what you folks think about to make
> modules such as always provide the same instance of a module when required
> within the same Racket or DrRacket session. Is it possible? Is it desirable?
> What when a module produces side effects (e.g. displayed output)?
> |#
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users

Posted on the users mailing list.