There is two file in a lein project a:core.clj and a.clj. core.clj have a function "foo",and a.clj have a function "add",which add two number,in folder a,enter
lein repl
in default,show me
a.core=>
I want to call function add in a.clj,use command "ns" or "in-ns",can switch to
a.a=>
but can't call add,
(add 1 2)
or
(a.a/add 1 2)
both tell me can't find add
ofcourse I can use (load-file "a.clj") in folder a,then I can use
(a.a/add 1 2)
but how to do it in "lein repl"?Thanks!
I have read https://clojure.org/guides/repl/navigating_namespaces, but can't fix it
I made a project that looks like so:
-rw------- 1 alan alan 873 Sep 25 03:18 project.clj
-rw------- 1 alan alan 201 Jan 4 20:21 src/clj/demo/core.clj
-rw------- 1 alan alan 50 Jan 4 20:20 src/clj/demo/fred.clj
The source files look like:
(ns demo.core)
(defn talk []
(prn :yabba))
(defn -main [& args]
(prn :main--enter)
(println "Hello World!")
(prn :main--leave))
and
(ns demo.fred)
(defn foo [& args]
(prn :foo))
You can manipulate it in the REPL using the require
function as follows. Note that this is different from the :require
clause in the (ns ...)
form.
Using (require ...)
in the REPL will cause the namespace to be loaded. AFTER the new NS is loaded, you may use in-ns
to switch from the current NS to the new one (i.e. from demo.core
to demo.fred
).
~/expr/demo > lein repl
Clojure 1.11.1
demo.core=> (talk)
:yabba
nil
demo.core=> (require '[demo.fred :as fred])
nil
demo.core=> (fred/foo)
:foo
nil
demo.core=> Bye for now!
and for completeness:
(defproject demo "0.1.0-SNAPSHOT"
:dependencies [
[org.clojure/clojure "1.11.1"]
[prismatic/schema "1.2.1"]
[tupelo "22.08.03"]
]
:global-vars {*warn-on-reflection* false}
:main ^:skip-aot demo.core
:source-paths ["src/clj"]
:java-source-paths ["src/java"]
:test-paths ["test/clj"]
:target-path "target/%s"
:compile-path "%s/class-files"
:clean-targets [:target-path]
:jvm-opts ["-Xms500m" "-Xmx2g"]
)
Please see this template project for my favorite way to set up a new project in Clojure.