On Thu, Jul 30, 2009 at 4:20 PM, Richard Newman<holyg...@gmail.com> wrote:
>
> The scenario here, though, is different: given an *existing* struct-
> map (which prints as {:foo 1 :bar 2} -- not a form), one cannot print
> it in such a way that the struct-map-ness is preserved when it is re-
> read. You can discover that it's a struct-map (type =>
> PersistentStructMap), but not what kind of struct-map it is.
>
> The same is true of sorted maps and sets, and assorted other data
> structures.

Are you aware of *print-dup* ?  It causes the printer to
preserve more of the specific type information than normal
printing:

user=> (binding [*print-dup* true] (prn (hash-map :a 1, :b 2)))
{:a 1, :b 2}
nil
user=> (binding [*print-dup* true] (prn (sorted-map :a 1, :b 2)))
#=(clojure.lang.PersistentTreeMap/create {:a 1, :b 2})
nil
user=> (binding [*print-dup* true] (prn {:a 1, :b 2}))
#=(clojure.lang.PersistentArrayMap/create {:a 1, :b 2})
nil

The #=() is an intentionally under-documented reader macro
specifically to allow such preservation.

Having said that, it's important to note it doesn't help
your original scenario, because *print-dup* doesn't support
struct maps (yet):

(-> (binding [*print-dup* true] (prn-str (struct foo 1 2)))
    java.io.StringReader. java.io.PushbackReader. read class)
java.lang.IllegalArgumentException: No matching method found: create
(NO_SOURCE_FILE:0)

sorted-maps work, but lose any custom sort-by fn:

(-> (binding [*print-dup* true] (prn-str (sorted-map :a 1, :b 2)))
    java.io.StringReader. java.io.PushbackReader. read class)
clojure.lang.PersistentTreeMap

(-> (binding [*print-dup* true]
      (prn-str (sorted-map-by #(compare %2 %1) :a 1, :b 2)))
    java.io.StringReader. java.io.PushbackReader. read)
{:a 1, :b 2}

--Chouser

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to