(def x (some-data-structure 1 2 3)) (def x2 (binding [*print-dup* true] (prn-str x))) ;; now x2 is a string serialization of x ;; You can save x2 to disk or whatever ;; Then to read it back in: (def x-clone (read-string x2))
(Note: you really shouldn't be programming in Clojure with a bunch of
def'ed vars like that...)
Unfortunately, Clojure records does not support this functionality quite just yet. There is defrecord2 that implements this serialization functionality for records though, described in this discussion in the Clojure group.
I had a second problem though, since I had records storing Incanter matrices, which are Parallel Colt matrices, and they don't
print-dupin a way that can be read back in with
read-string). So to solve both problems at the same time, we can just implement our own
print-dupfor the record we create with
print-dupis a multmethod).