(defn-update-body f args)
Return a defn
form where the body is updated by f
.
f
receives name
and body
and returns an updated body.
It supports all the defn
arguments (doctrings, metadata, multi-artities etc..)
It is meant to by used to create defn
like macros.
Based on https://blog.klipse.tech/clojure/2019/03/08/spec-custom-defn.html
Example:
We create a defn-try
macro that automatically wraps the function body in a try/catch
form.
(defn wrap-try [name body] `((try ~@body (catch :default ~'e (throw (str "Exception caught in function " ~name ": " ~'e))))))
(defmacro defn-try [& args] (defn-update-body wrap-try args))
We use defn-try
exactly like defn
:
(defn-try foo [a b] (+ a b))
is macroexpanded into:
(defn foo [a b] (try (+ a b) (catch java.lang.Throwable e (throw (str "Exception caught in function " "foo" ": " e)))))
Return a `defn` form where the body is updated by `f`. `f` receives `name` and `body` and returns an updated body. It supports all the `defn` arguments (doctrings, metadata, multi-artities etc..) It is meant to by used to create `defn` like macros. Based on https://blog.klipse.tech/clojure/2019/03/08/spec-custom-defn.html Example: We create a `defn-try` macro that automatically wraps the function body in a `try/catch` form. (defn wrap-try [name body] `((try ~@body (catch :default ~'e (throw (str "Exception caught in function " ~name ": " ~'e)))))) (defmacro defn-try [& args] (defn-update-body wrap-try args)) We use `defn-try` exactly like `defn`: (defn-try foo [a b] (+ a b)) is macroexpanded into: (defn foo [a b] (try (+ a b) (catch java.lang.Throwable e (throw (str "Exception caught in function " "foo" ": " e)))))
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close