[plt-scheme] Re: Clarifications on namespace.plt
Joel J. Adamson wrote:
> Now, how do I *instantiate* the module? Is there more to it than
> require?
This is what I ended up using:
#v+
(module enriched-eval mzscheme
(provide eval-in-enriched-ns
module-path->symbol
save-module)
(define-syntax save-module
(syntax-rules ()
[(_ variable)
(define variable
(let loop ([m (syntax-source-module #'variable)])
(if (symbol? m)
m
(let-values ([(name base) (module-path-index-split m)])
((current-module-name-resolver)
name
(if base (loop base) #f)
#f)))))]))
(define-syntax (eval-in-enriched-ns stx)
(syntax-case stx ()
[(eval-in-enriched-ns this-module (module-path ...)
body ...)
(with-syntax ([(module-symbol ...) (generate-temporaries
#'(module-path ...))])
#'(let ([new-ns (make-namespace)]
[module-symbol (module-path->symbol module-path
this-module)] ...)
(namespace-attach-module (current-namespace)
module-symbol
new-ns) ...
(parameterize ([current-namespace new-ns])
(namespace-require module-symbol) ...
body ...)))]))
(define (module-path->symbol module-path this-module)
((current-module-name-resolver) module-path this-module #f))
)
#v-
--
regards,
Jakub Piotr Cłapa