get a clojure function's code

不问归期 提交于 2020-01-01 04:20:08

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/4708516/get-a-clojure-functions-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!