#lang racket
(module m racket/base
(provide x)
(define x 11))
(require 'm)
(define ns (module->namespace ''m))
(eval 'x ns)
error meassage is given when running the above code:
module->namespace: unknown module in the current namespace
name: #resolved-module-path:'m
Why?
Just the formatted code with 4 spaces, no solution:
#lang racket
(module m racket/base
(provide x)
(define x 11))
(require 'm)
(define ns (module->namespace ''m))
(eval 'x ns)
- I think you need to
namespace-require
the module path prior to using module->namespace
.
module->namespace
needs a module path that is a path like "file.rkt"
— or, if you want it to work on a submodule m
, you need to use (submod "file.rkt" m)
.
- You may need to
namespace-syntax-introduce
the expression given to eval
, much like read-eval-print-loop
does. (Keep in mind that module->namespace
is intended to support getting a REPL “inside” a module, AFICT. It’s not something you’d likely use in the vast majority of “normal” Racket programs.)
So for example if file.rkt
is:
#lang racket/base
(define x 42)
Then your example.rkt
could be:
#lang racket/base
(define mod "file.rkt")
(namespace-require mod)
(parameterize ([current-namespace (module->namespace mod)])
(eval (namespace-syntax-introduce #'x)))
If you wanted to do this to a submodule in the same example.rkt
, then example.rkt
could be:
#lang racket/base
(module m racket/base
(define x 42))
(define mod '(submod "example.rkt" m))
(namespace-require mod)
(parameterize ([current-namespace (module->namespace mod)])
(eval (namespace-syntax-introduce #'x)))
2 Likes
p.s. I got distracted by the new Discourse auto-formatting features while typing this, and ended up going back and editing my post many times. So if you’re reading this only via email, it’s quite different from what ended up on this web site – sorry!
1 Like