Can someone explains the difference between print-method and print-dup once and for all?

327 views
Skip to first unread message

Didier

unread,
Aug 8, 2018, 8:01:09 PM8/8/18
to Clojure
Can someone explains the difference between print-method and print-dup once and for all?

Regards

Alex Miller

unread,
Aug 8, 2018, 9:18:41 PM8/8/18
to Clojure
This is glossing over some details, but print-method is for “human” printing (print, println).

print-dup is for printing readable data (pr, prn).

Often these are the same. Strings are a good example where they aren’t.

Didier

unread,
Aug 9, 2018, 12:57:02 AM8/9/18
to Clojure
Thanks, but I think I need the details. It seems pr, prn and company default to using print-method, and print-method implementations check for *print-readably*.

So how exactly would print-dup differ?

I ask specifically in the context of serializing Clojure to EDN and back. Would it be best to do so with print-dup true and print-readably true? What would be the expected pr-str settings?

David Bürgin

unread,
Aug 9, 2018, 2:10:33 PM8/9/18
to clo...@googlegroups.com
If your goal is to produce edn, go with pr/prn/*print-readably*, not
print-dup. print-dup output often cannot be read as edn:

(print-dup {:k "v"} *out*)
;; #=(clojure.lang.PersistentArrayMap/create {:k "v"})


--
David

Didier

unread,
Aug 10, 2018, 12:33:33 AM8/10/18
to Clojure
Thanks David. So if I need to extend edn to serialize custom types, I should extend print-method? And have it switch on print-readably? Where when true, prints a reabale edn literal #x/y ... for example, otherwise it prints as standard?

Would that be the correct way?

David Bürgin

unread,
Aug 10, 2018, 3:36:09 AM8/10/18
to clo...@googlegroups.com
On 10/08/18 06:33, Didier wrote:
> Thanks David. So if I need to extend edn to serialize custom types, I should extend print-method? And have it switch on print-readably? Where when true, prints a reabale edn literal #x/y ... for example, otherwise it prints as standard?
>
> Would that be the correct way?

Sounds reasonable to me. There is an old ticket which has pointers to
some relevant discussion I think:
https://dev.clojure.org/jira/browse/CLJ-1201


--
David
Reply all
Reply to author
Forward
0 new messages