将别名命名空间动态加载到另一个Clojure命名空间

时间:2015-03-13 13:44:44

标签: clojure namespaces

我正在尝试在运行时从文件加载命名空间。对于这个命名空间,我希望有一个共同的别名,所以我可以使用统一的限定名称访问该命名空间中的函数,该名称独立于加载文件的实际命名空间。

示例(不工作):

;; bar_a.clj
(ns bar-a)
(defn hello-world [] "hello world a")

;; bar_b.clj
(ns bar-b)
(defn hello-world [] "hello world b")


;; foo.clj
(ns foo)

(defn init [ns-name]
  (let [ns-symbol (symbol ns-name)]
    (require `[ns-symbol :as bar])
    (bar/hello-world)))       ;; => No such var bar/hello world
                              ;;    during runtime when calling `init`!!!

我已经尝试过各种各样的事情(load-fileload)并将require移到不同的地方。到目前为止没有运气。

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:4)

我认为问题在于符号的编译时解析对动态加载的命名空间不起作用。在您的示例中,bar/hello-world已解析编译时,但在调用函数时动态完成require。我不知道这个更漂亮的解决方案,但你可以尝试这样的事情:

(ns foo)

(defn init [ns-name]
  (require (symbol ns-name))
  (let [bar (find-ns (symbol ns-name))]
    ((ns-resolve bar 'hello-world))))

当然这不是很有效,因为每次调用该函数时都会解析命名空间和函数符号。但你应该能够理解。

相关问题