On Wednesday, 26 November 2014, Tassilo Horn <t...@gnu.org> wrote:

> Gary Verhaegen <gary.verhae...@gmail.com <javascript:;>> writes:
>
> Hi Gary,
>
> > You should probably read the documentation about tagged literals to
> > understand this better:
> >
> > http://clojure.org/reader#The%20Reader--Tagged%20Literals
>
> #user.P{:x 1, :y 2} is no tagged literal.  At least after defining
> (defrecord P [x y]), P records are printed like so and can be read back,
> but *data-readers* have no user.P entry and neither is
> *default-data-reader-fn* set.  So I conclude #ns.Rec{keyvals} is the
> standard read-syntax of records.
>
> Bye,
> Tassilo
>

You are indeed correct. I guess I was fooled by the syntactic similarity.

So, now that I've checked the reader source code (which I found
surprisingly easy to follow), to set the record straight, here is the
situation as I understand it (as of Clojure's master branch on github right
now):

* clojure.core/read (and clojure.core/read-string, which calls it) can read
the record literals as Tassilo said: as soon as a record has been defined
by defrecord, it is registered (it's actually looked up by its class name.
I have learned two additional interesting bits by reading the source:
first, there is actually another literal syntax for records, #user.P[1 2]
(in constructor order); the map syntax supports additional key/value pairs,
while the vector syntax requires an exact match. The second thing is that,
while clojure.core/read does consult *data-readers* upon encountering a
tagged literal (or look alike, such as a record), it only consults it if
the tag is not namespaced.
* On the other hand, the clojure.edn/read-string function has no knowledge
of protocols at all and relies solely on *data-readers* (and the passed in
reader map) for dispatch.

In conclusion, records may be read "natively" with clojure.core/read, but
to read them with clojure.edn/read you need to explicitly pass in the
record constructor fn as illustrated by Fluid Dynamics earlier. Thanks for
teaching me this, Tassilo!


PS: probably not recommended for production use, but the vector form of
"record literal" actually works for any class:

(read-string "#java.util.Date[12345678]")
; => #inst "1970-01-01T03:25:45.678-00:00"

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to