I have a Java class Vector2
that I'd like to persuade to "play nicely" with the Clojure reader.
(def a (vec2 1 2))
(print-str a)
=> "#<Vector2 [1 2]>"
Ideally I'd like the class to print out in a form that can be read by the Clojure reader. i.e. I'd like the following to return true:
(= a (read-string (print-str a)))
What is the best way of achieving this round-tripping capability?
You need to provide print-dup
and print-method
multimethods for your class/type.
Check out core.clj
Ex:
(import 'java.util.Hashtable)
(defmethod print-method Hashtable [x writer]
(binding [*out* writer]
(print (let [h (gensym)]
`(let [~h (Hashtable.)]
~@(map (fn [i]
`(.put ~h ~(str "\"" (.getKey i) "\"") ~(.getValue i) ) ) x) ~h))) ))
(def a (Hashtable.))
(.put a "a" 1)
(.put a "b" 2)
(= a (eval (read-string (print-str a))))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With