【发布时间】:2015-03-02 15:28:26
【问题描述】:
我正在使用以下片段创建命名空间,将当前命名空间中的给定符号列表及其值注入新命名空间并切换到新命名空间:
(defn in-ns-with-vars
"Switch to a new namespace (creating it if necessary) and
inject the given symbols into the new namespace
with the values they resolve to in the current namespace."
[new-ns-symbol & symbols-to-inject]
(let [n (create-ns new-ns-symbol)
current-value-of (fn [v] @(ns-resolve (the-ns *ns*) v))]
(do
(doseq [v symbols-to-inject]
(intern new-ns-symbol v (current-value-of v)))
(in-ns new-ns-symbol)
(clojure.core/refer 'clojure.core))))
是否存在实现此目的的现有机制?由于在脚本上下文中的使用,当前(初始)命名空间是静态未知的,所以我不能静态引用它。
【问题讨论】:
-
为什么不在新创建的命名空间中使用别名或引用
*ns*? -
这行得通;如果你把你的评论变成答案,我可以接受。
标签: clojure namespaces