为什么在使用 in-ns 移动到新命名空间时会丢失所有符号?

Why do I lose all symbols when using in-ns to move to a new namespace?

运行 Leiningen REPL 中的以下代码:

(in-ns 'my-namespace.core)
(+ 2 2)

导致此错误:

CompilerException java.lang.RuntimeException: Unable to resolve symbol: + in this context

为什么?

当您使用 in-ns 创建新的命名空间时,默认情况下不会引用核心命名空间 (clojure.core)。 "Referring" 命名空间意味着将它包含在您的命名空间中,这样您就可以引用该命名空间的符号作为您自己的符号。

仍然可以使用 clojure.core 中的符号,使用 完全限定名称 ,如下所示:

(clojure.core/+ 2 2)

解决方案是:

  1. 使用ns代替in-ns,像这样:(ns my-namespace.core)
  2. 参考clojure.core,像这样:(clojure.core/refer-clojure)