抑制REPL中原子保持的数据的打印? (或ref,代理,...) [英] Suppress the printing of the data an atom holds in the REPL? (or ref, agent, ...)
问题描述
以下是完全有效的Clojure代码:
The following is perfectly valid Clojure code:
(def a (atom nil))
(def b (atom a))
(reset! a b)
但是,在REPL中使用这样的东西很烦人:REPL将尝试打印此类引用的内容,只要你键入a或
However, it is annoying to work with such things in the REPL: the REPL will try to print the content of such references whenever you type a or b, and will, of course, generate a stack overflow error pretty quickly.
因此,有任何方法来控制/更改atom / refs /代理的打印行为。 Clojure?某些类型的循环检测将是很好的,但即使完全抑制deref'ed内容也是非常有用的。
So is there any way to control/change the printing behaviour of atoms/refs/agents in Clojure? Some kind of cycle detection would be nice, but even the complete suppression of the deref'ed content would be really useful.
推荐答案
您可以说
(remove-method print-method clojure.lang.IDeref)
可以从 print-method
中删除对derefable对象(Atoms,Refs等) ,使其打印如下:
to remove special handling of derefable objects (Atoms, Refs etc.) from print-method
, causing them to be printed like so:
user=> (atom 3)
#<Atom clojure.lang.Atom@5a7baa77>
或者,您可以添加更具体的方法来禁止打印内容一些特定的引用类型:
Alternatively, you could add a more specific method to suppress printing of contents of some particular reference type:
(defmethod print-method clojure.lang.Atom [a ^java.io.Writer w]
(.write w (str "#<" a ">")))
user=> (atom 3)
#<clojure.lang.Atom@4194e059>
这篇关于抑制REPL中原子保持的数据的打印? (或ref,代理,...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!