Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
;; Examples showing that when *print-meta* is bound to true, clojure.pprint/pprint appears
;; to show metadata on symbols, but not on collections. Can be misleading when only *some*
;; metadata is shown. Led me to believe the other metadata wasn't there until I double-checked
;; with pr.
(defn ppm [f m]
(binding [*print-meta* m]
(clojure.pprint/pprint f)))
(defn prm [f m]
(binding [*print-meta* m]
(pr f)))
(defn all [f]
(print "pp false: ") (ppm f false)
(print "pp true : ") (ppm f true)
(print "pr false: ") (prm f false) (println)
(print "pr true : ") (prm f true) (println))
(def f1 {:a 1 :b ^{:foo 8} [1 2 3]})
(all f1)
;; metadata only printed for pr with *print-meta* true
;; user=> (all f1)
;; pp false: {:a 1, :b [1 2 3]}
;; pp true : {:a 1, :b [1 2 3]}
;; pr false: {:a 1, :b [1 2 3]}
;; pr true : {:a 1, :b ^{:foo 8} [1 2 3]}
;; nil
(def f2 '(defn foo [^Integer x] (inc x)))
(all f2)
;; pprint with *print-meta* true prints some metadata, but not all of
;; it. Maybe only the metadata on symbols is pprint'ed, and not that
;; on collections?
;;user=> (all f2)
;;pp false: (defn foo [x] (inc x))
;;pp true : (defn foo [^Integer x] (inc x))
;;pr false: (defn foo [x] (inc x))
;;pr true : ^{:line 1, :column 10} (defn foo [^Integer x] ^{:line 1, :column 33} (inc x))
;;nil
(def f3 '(defn ^{:linter 8} foo [^Integer x] (inc x)))
(all f3)
;; again, pprint shows all metadata on symbols, but not on lists
;;user=> (all f3)
;;pp false: (defn foo [x] (inc x))
;;pp true : (defn ^{:linter 8} foo [^Integer x] (inc x))
;;pr false: (defn foo [x] (inc x))
;;pr true : ^{:line 1, :column 10} (defn ^{:linter 8} foo [^Integer x] ^{:line 1, :column 46} (inc x))
;;nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.