Created
September 22, 2010 06:40
-
-
Save Chouser/591243 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; This is very buggy. Barely works at all. | |
(defmacro delegate-proxy [bases args delegate & user-methods] | |
(let [x (gensym "delegate_") | |
user-method-names (set (map first user-methods)) | |
args-map (reduce (fn [tbl meth] | |
(update-in tbl [(symbol (.getName meth)) | |
(count (.getParameterTypes meth))] | |
conj | |
(.getParameterTypes meth))) | |
{} | |
(for [base bases, m (.getMethods (resolve base))] m))] | |
`(let [~x ~delegate] | |
(proxy ~bases ~args | |
~@user-methods | |
~@(for [[mname arg-counts] args-map | |
:when (not (user-method-names mname))] | |
`(~mname ~@(for [arg-count (keys arg-counts)] | |
(let [args (map #(symbol (str 'x %)) | |
(range arg-count))] | |
`([~@args] | |
(. ~x ~mname ~@args)))))))))) | |
(def dp-map (delegate-proxy [clojure.lang.IPersistentMap] [] | |
{:a :b, :c :d} | |
(empty [] {:not :empty}))) | |
(conj dp-map [:x :y]) | |
;=> {:x :y, :a :b, :c :d} | |
(empty dp-map) | |
;=> {:not :empty} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment