13

Is there a way in clojure to get a function's code after the function has been loaded?

Ie. without doing something like [untested]

(defmacro blat [x] `(do (def code ~(quote (mexpand-all x)))
                        ~x)))
(blat (defn func [abc] (...)))
Ellery Newcomer
  • 1,594
  • 1
  • 11
  • 23

1 Answers1

8

You can get the source of a symbol using the clojure.repl/source function. However, this only works if the var for which the symbol resolves to is in a .clj file on the classpath. You can't, for example, do this:

user=> (defn foo [x] x)
#'user/foo
user=> (require 'clojure.repl)
nil
user=> (clojure.repl/source foo)
Source not found
nil
Rayne
  • 31,473
  • 17
  • 86
  • 101