问题
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:
- Use
ns
instead ofin-ns
, like so:(ns my-namespace.core)
- 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