Skip to content

Instantly share code, notes, and snippets.

@saikyun
Last active January 24, 2019 19:22
Show Gist options
  • Save saikyun/a83bed977b2e16855c74776238ca1196 to your computer and use it in GitHub Desktop.
Save saikyun/a83bed977b2e16855c74776238ca1196 to your computer and use it in GitHub Desktop.
Replace print by Tims
(defonce old-print-method-impl print-method) ; the clojure.lang.MultiFn instance itself
(def ^:dynamic *overriding-print-method* false)
(defn print-method-override-dispatch [x writer]
(if *overriding-print-method*
(type x)
:default))
(defmulti print-method-override #'print-method-override-dispatch)
(defmethod print-method-override :default [x writer]
(old-print-method-impl x writer))
(defmethod print-method-override clojure.lang.Keyword [x writer]
(if (= x :snake)
(.Write writer "I bet you wanted to print \":snake\"")
(old-print-method-impl x writer)))
(alter-var-root #'print-method (constantly #'print-method-override))
;; ------------------------------------------------------------
(println [:dog :snake :cat])
;; => [:dog :snake :cat]
(binding [*overriding-print-method* true]
(println [:dog :snake :cat]))
;; => [:dog I bet you wanted to print ":snake" :cat]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment