How to override println behavior for reference types

Go To


I have a cyclic graph I created using dosync and ref-set. When I pass this to println I get a java.lang.StackOverflowError as I would expect, because it's effectively trying to print an infinitely-nested structure.

I found that if I do (str my-ref) it creates something that looks like vertex@23f7d873 and doesn't actually try to traverse the structure and print everything out, so this solves the problem in the immediate sense, but only helps when I'm very careful about what I'm printing to the screen. I'd like to be able to call (println my-graph) have it print the ref as some type of custom text (possibly involving str), and the other non-ref stuff normally.

Currently I have a custom print function that prints each element of the struct on its own and completely skips printing the ref. (It turns out that looking at vertex@23f7d873 is not actually very useful). This is awkward to use and hinders greatly doing casual inspection of stuff at the REPL and also prevents Emacs inspector from looking at stuff while I'm in a swank.core/break debug thingy.

One detail is the ref is actually a value in a defstruct that also contains some other stuff which I am trying to print normally.

So I'm wondering what path I should go down. I see these options:

  1. Figure out extend-type and apply the CharSequence protocol to my defstructed structure so that when it comes across a ref it works properly. This still requires a field-by-field inspection of the struct and a special case when it comes to the ref, but at least it localizes the problem to the struct and not to anything that contains the struct.
  2. Figure out how to override the CharSequence protocol when it comes across a ref. This allows even more localized behavior and allows me to view a cyclic ref at the REPL even when it's not inside a struct. This is my preferred option.
  3. Figure out how to do something with toString which I believe is called at some level when I do println. I'm most ignorant about this option. Pretty ignorant about the other ones too, but I've been reading Joy of Clojure and I'm all inspired now.

Likewise this solution should apply to print and pprint and anything else that would normally barf when trying to print a cyclic ref. What strategy should I employ?

thanks a lot for any input.

2012-04-04 04:49
by Sonicsmooth
fyi, the output of (str my-ref) is almost certainly the result of calling java.lang.Object#toString() as detailed here: - sw1nn 2012-04-04 10:56
Note that defstruct has been superseded by defrecord. Additionally, defstruct does not create a real type so it cannot participate in protocols - raek 2012-04-14 15:38
I realize now I actually was already using defrecord. Not sure why my original post said defstruct - Sonicsmooth 2012-04-17 05:23


What you want to do is create a new namespace and define your own print functions that models the way clojure prints objects, and defaults to clojure's methods.

In detail:

    Create an ns excluding pr-str and print-method. Create a multimethod print-method (copy exactly what is in clojure.core) Create a default method that simply delegates to clojure.core/print-method Create a method for clojure.lang.Ref that doesn't recursively print everything

As a bonus, if you are using clojure 1.4.0, you can use tagged literals so that reading in the output is also possible. You would need to override the *data-readers* map for a custom tag and a function that returns a ref object. You'd also need to override the read-string behavior to ensure binding is called for *data-readers*.

I've provided an example for

 (ns my.print
   (:refer-clojure :exclude [pr-str read-string print-method]))

 (defmulti print-method (fn [x writer]
         (class x)))

 (defmethod print-method :default [o ^ w]
       (clojure.core/print-method o w))

 (defmethod print-method [o ^ w]
       (.write w "#myprint/file \"")
       (.write w (str o))
       (.write w "\""))

 (defn pr-str [obj]
   (let [s (]
     (print-method obj s)
     (str s)))

 (defonce reader-map
   (ref {'myprint/file (fn [arg]
               ( arg))}))

 (defmacro defdata-reader [sym args & body]
     (alter reader-map assoc '~sym (fn ~args ~@body))))

 (defn read-string [s]
   (binding [*data-readers* @reader-map]
     (clojure.core/read-string s)))

Now you can call like so (println (my.print/pr-str circular-data-structure))

2012-04-04 14:29
by bmillare
Thanks, I was able to get this implemented except I got "Unable to resolve var: *data-readers*". After commenting that out I'm still left with having to call a special function when I know I'm printing out a circular reference. I was hoping to have clojure automagically print my reference the way you can in Python by overriding the __str__ or __repr__ functions - Sonicsmooth 2012-04-13 04:29
*data-readers* only works if you use clojure 1.4.0, also be sure to accept the answer if it helped you. You can also define clojure.core/print-methods for references, but then you are modifying the global print method, which may or may not be what you want - bmillare 2012-04-13 12:53
I see... I haven't tried 1.4.0 yet. Between this answer and this link: I was able to solve my problem. thanks - Sonicsmooth 2012-04-14 10:49


I found my solution -- just create a multimethod which overloads clojure.core/print-method for each particular type, and call the generic function from within the multimethod. In this case, each multimethod calls the generic print-graph-element which knows what to do with references (it just prints out the hash of the referenced object rather than trying to print out the value of the referenced object). In this case I'm assuming the dispatch function of print-method is just (type <thing>) so it dispatches on type, and that's how I'm writing the multimethod. I actually couldn't find any documentation that that's how the print-method dispatch works, but it sure seems to behave that way.

This solution allows me to just type the name of the object, such as v0 (which was cerated by (make-vertex) in the repl and print-method gets called by the repl, thus preventing my StackOverflow error.

(defmethod clojure.core/print-method vertex [v writer]
  (print-graph-element v))
(defmethod clojure.core/print-method edge [e writer]
  (print-graph-element e))
2012-04-14 10:42
by Sonicsmooth


If you just want to be able to print data structures with loops, try setting *print-level* to limit how deep the printer will descend.

user=> (def a (atom nil))
user=> (set! *print-level* 3)
user=> (reset! a a)
#<Atom@f9104a: #<Atom@f9104a: #<Atom@f9104a: #>>>

There is also a *print-length* var which can be useful when you are dealing with data with infinite length.

2012-04-14 15:31
by raek