Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get a clojure function's code

Tags:

clojure

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] (...)))
like image 892
Ellery Newcomer Avatar asked Jan 16 '11 22:01

Ellery Newcomer


1 Answers

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
like image 105
Rayne Avatar answered Oct 12 '22 05:10

Rayne