我想在clojure中的沙盒名称空间中运行加载文件
我是Clojure的新手,为了好玩/受教育,我正在为lein编写一个通用迁移框架。该系统必须做的一件事是从磁盘读取clojure文件,然后运行我想在clojure中的沙盒名称空间中运行加载文件,clojure,namespaces,Clojure,Namespaces,我是Clojure的新手,为了好玩/受教育,我正在为lein编写一个通用迁移框架。该系统必须做的一件事是从磁盘读取clojure文件,然后运行up功能或down功能。我认为这个文件可能应该在一个临时名称空间中进行计算,但我很难让它正常工作。以下是我目前掌握的情况: (def user-namespace (create-ns 'leiningen.generic-migrate.user-eval)) (defn load-migration-file [file] (binding [*
up
功能或down
功能。我认为这个文件可能应该在一个临时名称空间中进行计算,但我很难让它正常工作。以下是我目前掌握的情况:
(def user-namespace (create-ns 'leiningen.generic-migrate.user-eval))
(defn load-migration-file [file]
(binding [*ns* user-namespace]
(load-file (.getAbsolutePath file))
(keys (ns-publics *ns*))))
这给了我一个错误:
Unable to resolve symbol: defn in this context
我的问题是,使用加载文件,然后运行一个定义的函数,而不会有人覆盖我的命名空间中的内容的最佳方法是什么
(defmacro with-ns
"Evaluates body in another namespace. ns is either a namespace
object or a symbol. This makes it possible to define functions in
namespaces other than the current one."
[ns & body]
`(binding [*ns* (the-ns ~ns)]
~@(map (fn [form] `(eval '~form)) body)))
(defmacro with-temp-ns
"Evaluates body in an anonymous namespace, which is then immediately
removed. The temporary namespace will 'refer' clojure.core."
[& body]
`(try
(create-ns 'sym#)
(let [result# (with-ns 'sym#
(clojure.core/refer-clojure)
~@body)]
result#)
(finally (remove-ns 'sym#))))