Welcome! Please see the About page for a little more info on how this works.

0 votes
in Printing by
pprint currently doesn't print the names of defrecord types, instead printing just the underlying map. This is in contrast to pr-str/println. This ticket proposes that the behaviour of pprint is changed to match pr-str and println's.

More discussion at https://groups.google.com/forum/#!topic/clojure-dev/lRDG6a5eE-s


user=> (defrecord myrec [a b])
user.myrec
user=> (->myrec 1 2)
#user.myrec{:a 1, :b 2}
user=> (pr-str (->myrec 1 2))
"#user.myrec{:a 1, :b 2}"
user=> (println (->myrec 1 2))
#user.myrec{:a 1, :b 2}
nil
user=> (pprint (->myrec 1 2))
{:a 1, :b 2}
nil


*Approach:* Add new IRecord case to pprint's simple-dispatch mode. Extract guts of pprint-map to pprint-map-kvs and call it from both existing pprint-map and new pprint-record. Set multimethod preference for IRecord version. Added test.


user=> (pprint (->myrec 1 2))
#user.myrec{:a 1, :b 2}

*Patch:* CLJ-1890-pprint-records-2.patch

*Prescreened by:* Alex Miller

*Also see:* CLJS-1753

10 Answers

0 votes
by

Comment made by: desk@danielcompton.net

The fix for this will needed to be ported to ClojureScript too.

0 votes
by

Comment made by: steveminer@gmail.com

Added patch to pprint records with classname.

0 votes
by

Comment made by: steveminer@gmail.com

Open question: How should pprint handle a record that has a print-method defined for it? Should the print-method be used instead of the pprint default?

The current release and my patch do not consider the print-method when calling pprint.

0 votes
by
_Comment made by: alexmiller_

I do not think pprint should check for or use print-method. pprint has it's own simple-dispatch multimethod that you can extend, or it will ultimately fall through to pr (which can be extended via print-dup).

Example of the former:


user=> (defrecord R [a])
user=> (def r (->R 1))
user=> (pprint r)
{:a 1}

user=> (use 'clojure.pprint)
user=> (defmethod simple-dispatch user.R [r] (pr r))
#object[clojure.lang.MultiFn 0x497470ed "clojure.lang.MultiFn@497470ed"]
user=> (pprint r)
#user.R{:a 1}
0 votes
by

Comment made by: steveminer@gmail.com

Right, clojure.pprint/simple-dispatch is there for user code to customize pprint, independent of whatever they might do with print-method. No need to conflate the two. So the patch is ready to review.

0 votes
by

Comment made by: michaelblume

Patch does not appear to apply cleanly to current master.

0 votes
by

Comment made by: michaelblume

Doesn't apply cleanly to any of the beta tags, does apply cleanly to 1.8.0, but can't rebase without a conflict. Steve, you should probably fix that.

0 votes
by

Comment made by: steveminer@gmail.com

revised patch for current master (Clojure 1.9 beta4)

0 votes
by

Comment made by: steveminer@gmail.com

The old patch was prescreened but I guess the new patch should be reconsidered from scratch as namespaced maps were added to Clojure in between patches. By the way, I don't think there are any tests to cover pretty-printing namespaced maps. The only test I could find was for pr-str of a namespaced map. In any case, I tried to make sure the prefix logic was maintained so it should still work. The same test for pretty-printing records is used in both versions of the patch.

0 votes
by
Reference: https://clojure.atlassian.net/browse/CLJ-1890 (reported by desk@danielcompton.net)
...