repl/source 函数在 Clojure 中不起作用
repl/source function does not work in Clojure
我正在尝试使用文件而不是 REPL。
这是我的 clj 文件:
tests.my-clj-file.clj
(ns tests.my-clj-file
(:require [clojure.repl :as repl]))
(defn my-fn
[]
1)
(println (repl/source my-fn))
输出为:
找不到来源
无
如果你尝试 (doc repl/source)
,你会得到这样的结果(强调已添加):
Prints the source code for the given symbol, if it can find it.
This requires that the symbol resolve to a Var defined in a
namespace for which the .clj is in the classpath.
因此 clojure.repl/source
仅适用于从源文件加载的代码。如果你在 REPL 中输入代码(无论代码是否在文件中),它将不起作用。
只能从磁盘上的变量中读取源代码。
因此,如果您已经评估了它在 REPL 中加载的缓冲区,并且您无法使用 source
查看源代码。
完成阅读源代码的一种方法是将 my-fn
放在另一个文件中(例如,/my_other_clj_file.clj
):
(ns my-other-clj-file)
(defn my-fn
[]
1)
不计算缓冲区。
然后转到/tests/my_clj_file.clj
并评估:
(ns tests.my-clj-file
(:require [clojure.repl :as repl]
[other-file :refer [my-fn]))
(println (repl/source my-fn))
这确实正确地打印了来源。
(defn my-fn
[]
1)
nil
我正在尝试使用文件而不是 REPL。
这是我的 clj 文件:
tests.my-clj-file.clj
(ns tests.my-clj-file
(:require [clojure.repl :as repl]))
(defn my-fn
[]
1)
(println (repl/source my-fn))
输出为:
找不到来源
无
如果你尝试 (doc repl/source)
,你会得到这样的结果(强调已添加):
Prints the source code for the given symbol, if it can find it. This requires that the symbol resolve to a Var defined in a namespace for which the .clj is in the classpath.
因此 clojure.repl/source
仅适用于从源文件加载的代码。如果你在 REPL 中输入代码(无论代码是否在文件中),它将不起作用。
只能从磁盘上的变量中读取源代码。
因此,如果您已经评估了它在 REPL 中加载的缓冲区,并且您无法使用 source
查看源代码。
完成阅读源代码的一种方法是将 my-fn
放在另一个文件中(例如,/my_other_clj_file.clj
):
(ns my-other-clj-file)
(defn my-fn
[]
1)
不计算缓冲区。
然后转到/tests/my_clj_file.clj
并评估:
(ns tests.my-clj-file
(:require [clojure.repl :as repl]
[other-file :refer [my-fn]))
(println (repl/source my-fn))
这确实正确地打印了来源。
(defn my-fn
[]
1)
nil