repl / source函数在Clojure中不起作用

时间:2016-12-20 16:52:39

标签: clojure functional-programming lisp read-eval-print-loop

我正在尝试使用不是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))


输出结果为:

未找到来源

2 个答案:

答案 0 :(得分:1)

It is only possible to read the source from Vars that are on disk.

So if you have evaluated the buffer it is loaded in the REPL and you cannot view the source with source.

One way to accomplish reading the source is by placing my-fn in another file (e.g., /my_other_clj_file.clj):

(ns my-other-clj-file)

(defn my-fn
  []
  1)

Do not evaluate the buffer.

Then go to /tests/my_clj_file.clj and evaluate:

(ns tests.my-clj-file
  (:require [clojure.repl :as repl]
            [other-file :refer [my-fn]))

(println (repl/source my-fn))

This does correctly print the source.

(defn my-fn
  []
  1)
nil

答案 1 :(得分:0)

如果你尝试(doc repl/source),你会得到类似的内容(强调添加):

  

如果找到符号,则打印给定符号的源代码。   这要求符号解析为a中定义的Var   名称空间 .clj在类路径中。

所以clojure.repl/source仅适用于从源文件加载的代码。如果您在REPL中输入代码(无论代码是否在文件中),它都无法工作。