Skip to content

Instantly share code, notes, and snippets.

@Chouser
Created July 2, 2010 15:23
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/461507 to your computer and use it in GitHub Desktop.
Save Chouser/461507 to your computer and use it in GitHub Desktop.
; Create a proxy for which call methods return nil instead of throwing an
; exception.
;
; This is an ugly hack. Will probably break in future versions of clojure.
; Probably breaks under certain circumstances with current versions of clojure.
; May run over your dog.
(def nilfn (constantly nil))
(def nilfns
(reify
clojure.lang.IPersistentMap
clojure.lang.ILookup
(valAt [_ k] nilfn)))
(defmacro nilproxy [class-and-interfaces & [args]]
`(let [p# (proxy ~class-and-interfaces ~(or args []))]
(init-proxy p# nilfns)
p#))
; (.toString (nilproxy [Object])) ;=> nil
; (.clone (nilproxy [Object])) ;=> nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment