Print and read lists from a file in Clojure

I have a Clojure form data structure:

{: foo '("bar" "blat")}

and tried to write them to a file using various pr / prn / print. However, every time a structure is written as

{: foo ("bar" "blat")}

then when I try to read in it using the load file, I get an error message, for example:

java.lang.ClassCastException: java.lang.String cannot be added to clojure.lang.IF n (build-state.clj: 79)

presumably since the list is evaluated as a function call when it is read. Is there a way to write a structure with lists in their cited form?

thanks Nick

+4
source share
1 answer

Reverse to print is usually read, not loaded.

user> (read-string "{:foo (\"bar\" \"blat\")}") {:foo ("bar" "blat")} 

If you really need to print the downloadable code, you need to quote it twice.

 user> (pr-str '{:foo '("bar" "blat")}) "{:foo (quote (\"bar\" \"blat\"))}" user> (load-string (pr-str '{:foo '("bar" "blat")})) {:foo ("bar" "blat")} 
+8
source

All Articles