Controlling how a type instance appears in the REPL

36 views
Skip to first unread message

Outrovurt #

unread,
Apr 24, 2020, 11:41:25 PM4/24/20
to ClojureScript
If I create a type like this

(deftype MyType [x] ...)

is there any way I can control what is displayed at the REPL when I create a new instance of MyType?

At present all I see is:

cljs.user=> (MyType. 1)
#object[cljs.user.MyType]

It's this "#object[cljs.user.MyType]" which I'd like to customise. I've checked so much of the documentation and can't find anything which might let me display something else.

Thanks.

Outrovurt #

unread,
Apr 27, 2020, 6:55:45 AM4/27/20
to ClojureScript
Joy of joys, I found it completely by accident while looking for something completely different in this excellent online ClojureScript book:

https://funcool.github.io/clojurescript-unraveled/#printing

For reference (and in case the above link doesn't work for any reason), you need to implement IPrintWithWriter, like thus:

IPrintWithWriter
  (-pr-writer [p writer _]
    (-write writer (str "#<MyType " ,,, ">")))

on your type.

Simply brilliant!
Reply all
Reply to author
Forward
0 new messages