Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Monkeying with Clojure’s defmethod Macro
(defmacro defmethod-explicit
[multifn dispatch-val & fn-tail]
(let [[kw n & body] fn-tail]
(if (= :as kw)
`(let [~n ~dispatch-val]
(defmethod ~multifn ~dispatch-val ~body))
`(defmethod ~multifn ~dispatch-val ~fn-tail))))
(defmulti expl-mm (juxt first second))
(defmethod-explicit expl-mm [1 2] :as dv [_] (str dv))
(defmethod-explicit expl-mm [3 4] [s] (reverse s))
(expl-mm [1 2])
(expl-mm [3 4])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment