2

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

1
  • @Josh it's in clojure file not REPL. Commented Dec 20, 2016 at 19:25

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.