Reputation: 5231
I'm trying with files not REPL.
Here is my clj file:
tests.my-clj-file.clj
(ns tests.my-clj-file
(:require [clojure.repl :as repl]))
(defn my-fn
[]
1)
(println (repl/source my-fn))
The output is:
Source not found
nil
Upvotes: 2
Views: 316
Reputation: 5766
If you try (doc repl/source)
, you'll get something like this (emphasis added):
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.
So clojure.repl/source
only works with code that has been loaded from source files. It won't work if you enter the code in the REPL (regardless whether the code is in a file).
Upvotes: 0
Reputation: 10474
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
Upvotes: 1