首页 > 解决方案 > Clojure 将 var 添加到命名空间

问题描述

如何将 var 添加到 clojure 命名空间,即以后可以通过调用 ns-interns 来检索它?

不是(intern ns sym ...),因为实习生创建了一个 var 或重用任何 var sym 绑定的内容。当其他命名空间需要 ns 并保存对 ns 中 var 的引用时,这种区别很重要。我所要求的与 ns-unmap 正好相反。

标签: clojurenamespaces

解决方案


你不能只使用def吗?:

def
Creates and interns or locates a global var with the name of symbol and a
namespace of the value of the current namespace (*ns*). 

在代码中:

(def alpha)
(def beta :boo)
(intern 'tst.demo.core 'gamma :charlie)

(ns-interns 'tst.demo.core)
    => {alpha  #'tst.demo.core/alpha,  
        beta   #'tst.demo.core/beta
        gamma  #'tst.demo.core/gamma}

我不知道你在这里追求什么......?它们看起来都一样。


推荐阅读