Controlling how a type instance appears in the REPL

Skip to first unread message

Outrovurt #

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)

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.


Outrovurt #

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:

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

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

on your type.

Simply brilliant!
Reply all
Reply to author
0 new messages