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
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
On
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).
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-fnin another file (e.g.,/my_other_clj_file.clj):Do not evaluate the buffer.
Then go to
/tests/my_clj_file.cljand evaluate:This does correctly print the source.