Why do I lose all symbols when using in-ns to move

2020-04-17 07:46发布

问题:

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)