Reputation: 12883
For the sake of example, suppose I've wrapped a StringBuilder in a function so I can use it more easily in Clojure. I can easily make the no-arg version of the function call toString on the buffer e.g.
(defn builder
([^StringBuilder sb]
(fn
([] (.toString sb))
([& args]
(doseq [arg args]
(.append sb arg)))))
([] (builder (StringBuilder.))))
This is perfectly workable, however, I wonder how I could just override .toString() on the function itself so I could return the state of the StringBuilder or any other object that I have closed over.
Upvotes: 1
Views: 268
Reputation: 92012
You could try using https://github.com/technomancy/serializable-fn, which makes functions include their closed-over scope when they print. It will print the pr-str of the stringbuilder, though, which may or may not be exactly what you want.
Upvotes: 1