Last active
January 24, 2019 19:22
-
-
Save saikyun/a83bed977b2e16855c74776238ca1196 to your computer and use it in GitHub Desktop.
Replace print by Tims
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
(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