Search code examples
graphclojurecharsequencepprintcyclic-graph

How to override println behavior for reference types


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.


Solution

  • 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 java.io.File

     (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 ^java.io.Writer w]
           (clojure.core/print-method o w))
    
     (defmethod print-method java.io.File [o ^java.io.Writer w]
           (.write w "#myprint/file \"")
           (.write w (str o))
           (.write w "\""))
    
     (defn pr-str [obj]
       (let [s (java.io.StringWriter.)]
         (print-method obj s)
         (str s)))
    
     (defonce reader-map
       (ref {'myprint/file (fn [arg]
                   (java.io.File. arg))}))
    
     (defmacro defdata-reader [sym args & body]
       `(dosync
         (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))