如何覆盖引用类型的println行为 [英] How to override println behavior for reference types
问题描述
我有一个使用 dosync
和 ref-set
创建的循环图。当我把这个传递给 println
我得到一个 java.lang.StackOverflowError
,因为它是有效地尝试打印无限嵌套结构。
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.
我发现如果我做(str my-ref)
,它会创建一个看起来像 vertex @ 23f7d873
并没有真正尝试遍历结构和打印出所有的东西,所以这解决了问题的直接的意义,但只有当我非常小心, m打印到屏幕。我想能够调用(println my-graph)
将它打印成 ref
自定义文本(可能涉及 str
)和其他非ref资料。
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.
打印函数打印结构的每个元素,完全跳过打印 ref
。 (事实证明,看着 vertex @ 23f7d873
实际上并不是很有用)。这是尴尬的使用,阻碍了大量做随意检查的东西在REPL,也阻止Emacs检查员查看东西,而我在 swank.core / break
调试
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.
一个细节是 ref
实际上是一个 code>,也包含一些其他的东西,我试图正常打印。
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:
- 找出
extend-type
并应用< c $ c> $结构的
协议,以便在遇到CharSequence ref
它工作正常。这仍然需要逐个字段检查结构和一个特殊情况,当它涉及到ref
,但至少它本地化问题到结构,而不是 c> c $ cc> c code> c code code $ c 。这允许更多的本地化行为,并允许我查看REPL的循环引用,即使它不在结构内。这是我首选的选项。 - 找出如何使用
toString
做一些事情,我相信在某些级别调用println
。我对这个选项最无知。对于其他人也很无知,但我一直在阅读Joy of Clojure
,我现在都受到启发。
- Figure out
extend-type
and apply theCharSequence
protocol to mydefstruct
ed structure so that when it comes across aref
it works properly. This still requires a field-by-field inspection of the struct and a special case when it comes to theref
, but at least it localizes the problem to the struct and not to anything that contains the struct. - Figure out how to override the
CharSequence
protocol when it comes across aref
. 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. - Figure out how to do something with
toString
which I believe is called at some level when I doprintln
. I'm most ignorant about this option. Pretty ignorant about the other ones too, but I've been readingJoy of Clojure
and I'm all inspired now.
同样,此解决方案应该应用于 print
和 pprint
当尝试打印循环引用时通常为barf。我应该采用什么策略?
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?
非常感谢任何输入。
推荐答案
你想要做的是创建一个新的命名空间,并定义自己的打印函数,模拟Clojure打印对象的方式,默认为clojure的方法。
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.
:
创建一个不包括pr-str和print-method的ns。
创建一个多重方法打印方法(完全复制clojure.core中的内容)
创建一个简单地委托给clojure.core / print-method的默认方法
为clojure.lang创建一个方法.Ref不递归打印所有内容
-
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
作为奖励,如果你使用clojure 1.4.0,你可以使用标记的文字,输出也是可能的。您需要为自定义标记和返回引用对象的函数覆盖 * data-readers *
映射。您还需要重写读取字符串行为,以确保为 * data-readers *
调用绑定。
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*
.
我为java.io.File提供了一个示例
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)))
现在你可以这样调用(println(my.print / pr-str circular-data-structure))
这篇关于如何覆盖引用类型的println行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!