0
votes

In a lein repl, whenever I do

(in-ns 'some-namespace-other-than-name.core)

clojure.core is not included by default.

To explain, initially I found myself getting caught by this like so

db.core=> (in-ns 'db.seed)
#object[clojure.lang.Namespace 0x12738ef5 "db.seed"]
db.seed=> (use 'environ.core)
Syntax error compiling at (form-init7774277424301430706.clj:1:1).
Unable to resolve symbol: use in this context

I just found how to fix this:

db.seed=> (clojure.core/use 'clojure.core)
nil
db.seed=> (use 'environ.core)
nil

My question is, it seems that clojure.core is included automatically in the namespace myapp.core, but not in any other namespaces i might switch to in the repl. (yet, obviously those namespaces do have access to clojure.core when i run the program from the core namespace).

Is that just a leiningen default?

Wondering what piece of understanding or usage I'm missing here.

Is it that when we run a program, only myapp.core needs access to clojure.core, and the namespaces your .core will utilise do not themselves need access to clojure.core, because they are, when executed, merely imports into your app's .core? Hence switching to some other namespace to run things is in essence a bit artificial?

I looked at the docs for the :main key in project.clj but couldn't find an answer.

1
There is also refer-clojure, see the explanation here: clojure.org/guides/repl/…glts
Interesting. So issuing (clojure.core/refer-clojure) seems to have similar effect to issuing (clojure.core/use 'clojure.core), when in a namespace where core is absent.mwal
If of interest, the method I've been using when direct at the repl is to issue like (require 'db.series :verbose :reload) to reload a file I've been working on, then simply call functions in that other (now reloaded) namespace always by fully qualifying them, like (pprint (db.series/app-state ...)), i.e. not bothering to switch to the other namespace at all. At least this works without surprises.mwal

1 Answers

3
votes

See these two references in ClojureDocs.org:

I think it's easier to just use ns.

There is also a nice blog posting by 8thlight. You should also be sure to read How to ns.


Example

Suppose I have a main ns demo.core and a testing ns tst.demo.core. I access from the repl:

~/expr/demo > lein repl
demo.core=> (in-ns 'tst.demo.core)
#object[clojure.lang.Namespace 0x6a2b14a1 "tst.demo.core"]
tst.demo.core=> (use 'demo.core)
Syntax error compiling at (/tmp/form-init10207825906790622351.clj:1:1).
Unable to resolve symbol: use in this context

So the above shows the problem, that clojure.core is not referred into the ns by in-ns.

Alternatively, just use ns:

~/expr/demo > lein repl
demo.core=> (ns tst.demo.core)       ; note absence of single-quote!
nil
tst.demo.core=> (use 'demo.core)     ; all clojure.core is automatically refered
nil