gpt4 book ai didi

Clojure/ClojureScript : How to plug in custom print implementation for EDN tag literals?

转载 作者:行者123 更新时间:2023-12-03 07:50:01 25 4
gpt4 key购买 nike

我有一条记录,一个实例,并将其打印到 EDN 字符串:

(ns my)
(defrecord Ref [type id])
(def rich (->Ref :Person 42)
(pr-str rich)

我想要“#my.Ref [:Person 42]

但我得到“#my.Ref{:type :Person, :id 23}”

如何插入自定义实现来打印 Ref 实例?

最佳答案

在 CLJS 中,这是通过 cljs.core/IPrintWithWriter 协议(protocol)完成的。

(extend-type Ref
IPrintWithWriter
(-pr-writer [this writer opts]
(-write writer "#my.Ref ")
(-pr-writer [(:type this) (:id this)] writer opts)))

在 CLJ 中,它是通过 clojure.core/print-method 多方法完成的。

(defmethod print-method Ref [this writer]
(.write writer "#my.Ref ")
(print-method [(:type this) (:id this)] writer))

关于Clojure/ClojureScript : How to plug in custom print implementation for EDN tag literals?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77460039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com