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

+1 vote
in ClojureScript by
Hi there!
I am having troubles making the cljs pretty print (cljs.pprint/pprint) behave the same way as the regular cljs or clj print function when it goes down to tagged elements that are by default used to denote custom records.

See example below - *pr, pr-str, print* and *print-str* functions all use the default approach toward creating (edn-like) tagged element for MyRecord and all produce the same result:
*#cljs.user.MyRecord{:value "a"}*

On the other hand *pprint* just ignores the record's tag and simply just traverses/prints it as a map:
*{:value "a"}*

Is there some setting and/or parameter in cljs.pprint namespace I am missing? I looked briefly at the code, but it seems it uses print-str by default - so maybe it just traverses the graph depth-first and does not check for every node's type? At this stage this seems like a bug to me as the expected behavior of the pprint function is that it would behave the same way print and other core functions do.


cljs.user=> (defrecord MyRecord [value])
cljs.user=> (pr (MyRecord. "a"))
#cljs.user.MyRecord{:value "a"}
cljs.user=> (pr-str (MyRecord. "a"))
"#cljs.user.MyRecord{:value \"a\"}"
cljs.user=> (print (MyRecord. "a"))
#cljs.user.MyRecord{:value a}
cljs.user=> (print-str (MyRecord. "a"))
"#cljs.user.MyRecord{:value a}"


cljs.user=> (cljs.pprint/pprint (MyRecord. "a"))
{:value "a"}
("{:value \"a\"}\n")
cljs.user=> (with-out-str (cljs.pprint/pprint (MyRecord. "a")))
"{:value \"a\"}\n"

According to github the head revision of the cljs.pprint namespace has not changed since 1.7.28 so I'd assume all versions up to the current one are affected.

Thanks for help!

5 Answers

0 votes

Comment made by: dnolen

Patch must supply a test case.

0 votes
_Comment made by: anmonteiro_

Not sure if this is a bug. Running this at a Clojure REPL produces the same results:

user=> (defrecord MyRecord [value])
user=> (require '[clojure.pprint :as pprint])
user=> (pprint/pprint (MyRecord. "a"))
{:value "a"}
0 votes

Comment made by: miro

Good catch, that is indeed interesting... but I'd still argue that this is definitely a bug (which just seems to be present in clj as well) - pretty print should work in the same way as print does, only prettier :) (aka more readable). It should not have its own idiosyncrasies. Every other print function (even including spit) works this way, so I can not imagine what would be the reason (or a use case) for pprint not to honor tagged literals specification of clojure and print things its own way.

0 votes

Comment made by: steveminer@gmail.com

There's a new patch for CLJ-1890. If it's accepted for Clojure, it can be ported to CLJS.

0 votes
Reference: https://clojure.atlassian.net/browse/CLJS-1753 (reported by alex+import)