How to override println behavior for reference types

佐手、 提交于 2019-12-05 09:38:19

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!