Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Reify a protocol by giving a generic implementation function that will be called for all protocol methods. Like a proxy.
(ns reify-generic
"reify a protocol such that every method is delegated to a specified method")
(defmacro reify-generic
"Reify the given protocols. Every method of any protocol is implemented such
that f is called as (apply f protocol-method args)
(defprotocol Calculator
(add [this x y])
(sub [this x y])
(mul [this x y])
(div [this x y]))
(defn test-run []
(let [printer (reify-generic (fn [method this & args] (apply println method args)) Calculator)]
(add printer 1 2)
(sub printer 1 3)))
[f & protocols]
~@(apply seq (map (fn [proto]
(apply concat [proto]
(map (fn [{name :name arglists :arglists}]
(map (fn [arglist] `(~name ~arglist (~f ~(keyword name) ~@arglist)))
(-> proto resolve deref :sigs vals))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment