Clojure *ns*意外计算为';用户';名称空间使用';lein run';,但在使用';莱恩·雷普';
我正在尝试获取与实际运行的代码关联的名称空间 我使用“lein new app test”创建了一个模板项目:Clojure *ns*意外计算为';用户';名称空间使用';lein run';,但在使用';莱恩·雷普';,clojure,Clojure,我正在尝试获取与实际运行的代码关联的名称空间 我使用“lein new app test”创建了一个模板项目: (ns test.core (:gen-class)) (defn -main [& args] (println (ns-name *ns*))) 使用repl时,*ns*的计算结果为test.core: echo "(-main)" | lein repl nREPL server started on port 37435 on hos
(ns test.core
(:gen-class))
(defn -main
[& args]
(println (ns-name *ns*)))
使用repl时,*ns*的计算结果为test.core:
echo "(-main)" | lein repl
nREPL server started on port 37435 on host 127.0.0.1 - nrepl://127.0.0.1:37435
REPL-y 0.4.4, nREPL 0.7.0
Clojure 1.10.1
OpenJDK 64-Bit Server VM 1.8.0_265-b01
Docs: (doc function-name-here)
(find-doc "part-of-name-here")
Source: (source function-name-here)
Javadoc: (javadoc java-object-or-class-here)
Exit: Control+D or (exit) or (quit)
Results: Stored in vars *1, *2, *3, an exception in *e
test.core=> (-main)
test.core
nil
test.core=> Bye for now!
但在使用lein run时,我得到了用户名称空间:
lein run
user
为什么会有这样的行为?
如何获得repl中这样的代码的名称空间?需要注意的几点:
绑定到表达式求值的命名空间*ns*
是默认名称空间user
- 当您从
命名空间调用用户
时,(test.core/-main)
绑定到*ns*
,而不是用户
test.core
-main
的命名空间,可以使用顶级表达式:
(ns test.core)
(def当前ns*ns*)
(定义-主参数[&args]
(println(ns名称当前ns)))
或者使用var元数据:
(ns test.core)
(定义-主参数[&args]
(println(ns名称:ns(meta#'-main(#'))))
您可能会发现这个问题也很有趣: