Cannot attach submodule to namespace

20 views
Skip to first unread message

Sage Gerard

unread,
Apr 16, 2021, 1:29:33 PM4/16/21
to Racket Users
Why does this raise "namespace-attach-module: module not declared (in
the source namespace)"?

I expected that the `restricted` submodule would be both declared and
instantiated by the time control reached `namespace-attach-module`.

(module anon racket/base
  (module restricted racket/base
    (provide #%app #%datum #%top hello)
    (define (hello h) h))
  (require 'restricted)
  (define ns (make-empty-namespace))
  (define-namespace-anchor a)
  (namespace-attach-module (namespace-anchor->namespace a) ''restricted ns)
  (displayln (eval '(hello "world") ns)))

--
~slg


Matthew Flatt

unread,
Apr 16, 2021, 1:51:21 PM4/16/21
to Sage Gerard, Racket Users
The name `'restricted` is allowed as a shorthand in `require` because
`require` knows what module it's in. The `namespace-attach-module`
function does not try to infer a module context from the namespace
argument; it uses the namespace argument only for its registry. So, you
need to use the full name of the submodule.

The `quote-module-path` form expands to a full name using its syntactic
context (i.e., the enclsoing module's name), so

(namespace-attach-module (namespace-anchor->namespace a)
(quote-module-path restricted)
ns)

is probably what you want.

Sage Gerard

unread,
Apr 16, 2021, 2:02:56 PM4/16/21
to Racket Users
Yes. The error changed to "hello: unbound identifier, also no #%app ..."
for me, so I also had to add `(namespace-require (quote-module-path
restricted) ns)` to get it working.

Were you expecting that I had to do that too? The docs for the
`make-base-*-namespace` procedures make it sound like attaching is
sufficient.
--
~slg


Matthew Flatt

unread,
Apr 16, 2021, 2:11:56 PM4/16/21
to Sage Gerard, Racket Users
Yes, attaching a module instances doesn't `require` it anywhere.

(Sorry --- I didn't look at your program closely enough to work out
whether you meant to require it, start out with a non-empty namespace,
or something else.)
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email
> to racket-users...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/e11dff02-0a27-06bd-9b4c-128f15e65
> 849%40sagegerard.com.

Sage Gerard

unread,
Apr 16, 2021, 2:13:00 PM4/16/21
to racket...@googlegroups.com
No worries. Thank you for the help as always.
> To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/20210416121152.2b0%40sirmail.smtps.cs.utah.edu.

--
~slg


Reply all
Reply to author
Forward
0 new messages