I’m struggling with building a reader function for a tagged literal that will yield a clojure.core.Vec
-which, when initialized to hold :byte
, is a pretty close stand-in for a Java native byte array but with the nice property of being immutable and following Clojure equality semantics.
Regardless of my reader function, the resulting value is always a clojure.lang.PersistentVector
-which is not at all the same thing.
Here’s a quick reader function you can try that looks like it should work:
(defn read-bytes [bs] (apply vector-of :byte (map byte bs)))
Then add an appropriate data_readers.clj
entry:
{cch/bytes cch.bytes/read-bytes}
Then, from the REPL, type in
(class #cch/bytes [-1 0 1])
I am surprised by the result, especially since this yields the expected answer:
(read-string "#cch/bytes [1 0 -1]")
It’s as though the Clojure reader cycles through the print/read process a second time after the reader function has done its job. But even defining print-dup
for clojure.core.Vec
doesn’t seem to resolve the problem. Here’s my attempt:
(defmethod print-dup clojure.core.Vec
[^clojure.core.Vec v ^java.io.Writer w]
(.write w "#cch/bytes ")
(.write w (str v)))
In fact, it doesn’t seem to matter what I use for the print-dup implementation -Clojure always prints a clojure.core.Vec
as a clojure.lang.PersistentVector
.
So I guess there are really two questions: 1. Why can’t I supply a print-dup
for clojure.core.Vec
? and 2. Why is my tagged literal reader function not sufficient to yield a clojure.core.Vec
?