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

浪尽此生 提交于 2020-01-11 10:47:09

问题


Running the following code in a Leiningen REPL:

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

results in this error:

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

Why?


回答1:


When you create a new namespace using in-ns, the core namespace (clojure.core) is not referred by default. "Referring" a namespace means including it in your namespace in such a way that you can refer to that namespace's symbols as your own.

It is still possible to use symbols from clojure.core using fully qualified names, like so:

(clojure.core/+ 2 2)

The solution is to either:

  1. Use ns instead of in-ns, like so: (ns my-namespace.core)
  2. Refer clojure.core, like so: (clojure.core/refer-clojure)


来源:https://stackoverflow.com/questions/43839443/why-do-i-lose-all-symbols-when-using-in-ns-to-move-to-a-new-namespace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!