Это было мое сообщение; приятно встретить вас ;-) Кстати, ссылки, приведенные в этой теме для ответов, были отличным чтением; поэтому, если вам интересно, вы можете потратить время на их чтение. Вернемся к вашему вопросу, хотя source
, похоже, работает для кода, загруженного через файл, но он не работает во всех случаях. Я думаю, в частности, он не работает для функций, определенных в repl.
user=> (def foo (fn [] (+ 2 2)))
#'user/foo
user=> (source foo)
Source not found
nil
user=> (defn foo2 [] (+ 2 2))
#'user/foo2
user=> (source foo2)
Source not found
nil
Копаем немного ...
user=> (source source)
(defmacro 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.
Example: (source filter)"
[n]
`(println (or (source-fn '~n) (str "Source not found"))))
nil
user=> (source clojure.repl/source-fn)
(defn source-fn
"Returns a string of 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. Returns nil if
it can't find the source. For most REPL usage, 'source' is more
convenient.
Example: (source-fn 'filter)"
[x]
(when-let [v (resolve x)]
(when-let [filepath (:file (meta v))]
(when-let [strm (.getResourceAsStream (RT/baseLoader) filepath)]
(with-open [rdr (LineNumberReader. (InputStreamReader. strm))]
(dotimes [_ (dec (:line (meta v)))] (.readLine rdr))
(let [text (StringBuilder.)
pbr (proxy [PushbackReader] [rdr]
(read [] (let [i (proxy-super read)]
(.append text (char i))
i)))]
(read (PushbackReader. pbr))
(str text)))))))
nil
Так что, похоже, он пытается загрузить исходный файл из класса, чтобы попытаться выплюнуть его для вас. Одна вещь, которую я узнал при работе с Clojure, - это то, что 9 раз из 10 полезно посмотреть на источник.