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

+1 vote
in Printing by

To my understanding, Clojure's pr function prints objects so that when read back will be = to the original object. My main source for this come s from this talk https://youtu.be/pbodL96HM28?t=216 from Clojure core developer Stuart Sierra ( I cannot find any docs to say what guarantees pr offers).

If this is correct, then pr is not working for java.sql.Timestamp and java.util.Calendar. For example, this returns false.

(let [d (java.sql.Timestamp. 1)]
(= d
(read-string
  (pr-str d))))

Further, Clojure's documentation says , print-dup, when true, means that objects will be printed in a way that preserves their type when read in later

This is not the case for java.sql.Timestamp and java.util.Calendar

`
(binding [*print-dup* true]
(prn (java.sql.Timestamp. 1)))
=> #inst "1970-01-01T00:00:00.001000000-00:00"

As well as being interesting to get some answers to the above, understanding these issues might give some basis for Clojure's support of java.time date types as tracked by https://clojure.atlassian.net/browse/CLJ-2224 .

I understand the above behaviour can be changed by users, but I am asking about the default implementations.

1 Answer

0 votes
by

This is the general intent, but both the read and print systems are open and extensible, and this is mostly done on the basis of Java type (for print) or tag (for read).

The choice was made when the #inst tagged literal was created for several JDK types to print as #inst. I wasn't there for that, so don't really know that I can explain the reasoning.

The default reader for #inst will read to a java.util.Date, but https://clojure.github.io/clojure/clojure.instant-api.html has reader functions available to read into Calendar or java.sql.Timestamp. These can be bound either globally or on a per-read basis if needed (the original changelog at https://github.com/clojure/clojure/blob/master/changes.md#211-instant-literals has examples).

by
Thanks Alex. I guess it'll remain a mystery for now As you say the most important thing is it's easy for Clojure users to change the reading and printing setup to suit their needs.
...