The following is perfectly valid Clojure code:
(def a (atom nil))
(def b (atom a))
(reset! a b)
it is even useful in situations where back references are needed.
However, it is annoying to work with such things in the REPL: the REPL will try to print the content of such references whenever you type a or b, and will, of course, generate a stack overflow error pretty quickly.
So is there any way to control/change the printing behaviour of atoms/refs/agents in Clojure? Some kind of cycle detection would be nice, but even the complete suppression of the deref'ed content would be really useful.
You can say
(remove-method print-method clojure.lang.IDeref)
to remove special handling of derefable objects (Atoms, Refs etc.) from print-method
, causing them to be printed like so:
user=> (atom 3)
#<Atom clojure.lang.Atom@5a7baa77>
Alternatively, you could add a more specific method to suppress printing of contents of some particular reference type:
(defmethod print-method clojure.lang.Atom [a ^java.io.Writer w]
(.write w (str "#<" a ">")))
user=> (atom 3)
#<clojure.lang.Atom@4194e059>
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