Skip to content

Commit 92b4fc7

Browse files
committed
make j.u.Collections print-readably (pr, prn) like their Clojure variants, print* and *print-dup* behavior unchanged
1 parent 2430b7a commit 92b4fc7

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

src/clj/clojure/core_print.clj

+39-2
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,16 @@
8787
(print-args o w)
8888
(.write w ")"))
8989

90-
(defmethod print-method Object [o, ^Writer w]
90+
(defn- print-object [o, ^Writer w]
9191
(.write w "#<")
9292
(.write w (.getSimpleName (class o)))
9393
(.write w " ")
9494
(.write w (str o))
9595
(.write w ">"))
9696

97+
(defmethod print-method Object [o, ^Writer w]
98+
(print-object o w))
99+
97100
(defmethod print-method clojure.lang.Keyword [o, ^Writer w]
98101
(.write w (str o)))
99102

@@ -214,6 +217,40 @@
214217
(print-map m print-dup w)
215218
(.write w ")"))
216219

220+
;; java.util
221+
(prefer-method print-method clojure.lang.IPersistentCollection java.util.Collection)
222+
(prefer-method print-method clojure.lang.IPersistentCollection java.util.RandomAccess)
223+
(prefer-method print-method java.util.RandomAccess java.util.List)
224+
(prefer-method print-method clojure.lang.IPersistentCollection java.util.Map)
225+
226+
(defmethod print-method java.util.List [c, ^Writer w]
227+
(if *print-readably*
228+
(do
229+
(print-meta c w)
230+
(print-sequential "(" pr-on " " ")" c w))
231+
(print-object c w)))
232+
233+
(defmethod print-method java.util.RandomAccess [v, ^Writer w]
234+
(if *print-readably*
235+
(do
236+
(print-meta v w)
237+
(print-sequential "[" pr-on " " "]" v w))
238+
(print-object v w)))
239+
240+
(defmethod print-method java.util.Map [m, ^Writer w]
241+
(if *print-readably*
242+
(do
243+
(print-meta m w)
244+
(print-map m pr-on w))
245+
(print-object m w)))
246+
247+
(defmethod print-method java.util.Set [s, ^Writer w]
248+
(if *print-readably*
249+
(do
250+
(print-meta s w)
251+
(print-sequential "#{" pr-on " " "}" (seq s) w))
252+
(print-object s w)))
253+
217254
;; Records
218255

219256
(defmethod print-method clojure.lang.IRecord [r, ^Writer w]
@@ -241,7 +278,7 @@
241278
(print-meta s w)
242279
(print-sequential "#{" pr-on " " "}" (seq s) w))
243280

244-
(def ^{:tag String
281+
(def ^{:tag String
245282
:doc "Returns name string for char or nil if none"
246283
:added "1.0"}
247284
char-name-string

0 commit comments

Comments
 (0)