Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 329
Sonicsmooth Avatar asked Apr 04 '12 04:04

Sonicsmooth


3 Answers

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))

like image 143
bmillare Avatar answered Nov 16 '22 03:11

bmillare


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))
like image 4
Sonicsmooth Avatar answered Nov 16 '22 03:11

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/a
user=> (set! *print-level* 3)
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.

like image 1
raek Avatar answered Nov 16 '22 02:11

raek