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

94 views Asked by At

I have a record, an instance of and I print it to EDN string:

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

I would like to get "#my.Ref [:Person 42].

But I get "#my.Ref{:type :Person, :id 23}".

Sow how can I plug in my custom implementation for printing instances of Ref?

1

There are 1 answers

2
Thomas Heller On

In CLJS this is done via the 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)))

In CLJ it is done via the clojure.core/print-method multimethod.

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