If I use pprint and read-string to serialize and deserialize data
w/o function references it works o.k.  This is clojure 1.3.

e.g. save something to disk, restart the vm, read it in ok.

  user=> (def foo {:a 1 :b 2 :c ["more" "stuff"]})
  user=> (def output (java.io.FileWriter. "foo.clj"))
  user=> (clojure.pprint/pprint foo output)
  user=> (.close output)

  $ cat foo.clj
  {:a 1, :b 2, :c ["more" "stuff"]}

  user=> (def foo (read-string (slurp "foo.clj"))) ;;; all good

However, if there's a function in that data structure, I cannot
read the forms back in.

  user=> (def foo (assoc foo :d clojure.core/+))
  user=> foo
  {:d #<core$_PLUS_ clojure.core$_PLUS_@1ce00b4>, :a 1, :b 2, :c
["more" "stuff"]}
  user=> ;; save to file as above...

  $ cat foo.clj
  {:d #<core$_PLUS_ clojure.core$_PLUS_@1ce00b4>,
   :a 1,
   :b 2,
   :c ["more" "stuff"]}

  user=> (def foo (read-string (slurp "foo.clj")))
  java.lang.RuntimeException: java.lang.Exception: Unreadable form
(NO_SOURCE_FILE:1)

As an experiment, I edited the file and changed the object
reference to the fully qualified name of the function, I could
read the structure in, and it appears to be a function (according
to clojure test), but it prints differently, and it does not
behave like + anymore, instead I simply get the last argument
back.

  $ cat foo.clj
  {:d clojure.core/+
   :a 1,
   :b 2,
   :c ["more" "stuff"]}

  user=> (def foo (read-string (slurp "foo.clj")))
  #'user/foo
  user=> (require 'clojure.test)
  nil
  user=> (clojure.test/function? (foo :d))
  true
  user=> +
  #<core$_PLUS_ clojure.core$_PLUS_@1a19458>
  user=> (foo :d)  ;;; notice that it prints differently
  clojure.core/+
  user=> ((foo :d) 4 5)  ;;; what don't I understand about this?
  5
  user=> (def fun (foo :d))
  #'user/fun
  user=> (fun 4 5)  ;;; didn't expect different...
  5
  user=> (+ 4 5)
  9

I'm not sure what I don't undertand about what I had going on in
the last example, but that isn't my primary question.

My primary question is, how do I serialize and deserialize data with
function references in it?

Thanks

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