Skip to content

Instantly share code, notes, and snippets.

@Chouser
Created September 22, 2010 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chouser/591243 to your computer and use it in GitHub Desktop.
Save Chouser/591243 to your computer and use it in GitHub Desktop.
;; 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}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment