Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a custom print method for clojurescript

To customise printing of records, I usually add a print-method:

(defrecord Op [type value]
  Object
  (toString [op]
    (str [type value])))

(defmethod print-method Op
  [v w]
  (.write w (str v)))

but when I'm in clojurescript, I'm getting an error:

Use of undeclared Var synchrony.operation/print-method at line 11

how would I do this in cljs?

like image 768
zcaudate Avatar asked Mar 21 '17 00:03

zcaudate


1 Answers

I think the way to do this in ClojureScript is to extend the IPrintWithWriter protocol to your object, e.g.

(extend-protocol IPrintWithWriter
  mycool.newObj
  (-pr-writer [new-obj writer _]
    (write-all writer "#myObj \"" (:details new-obj) "\"")))

I can't find much official documentation on this, so there may be another/better way to do this.

like image 107
Daniel Compton Avatar answered Nov 15 '22 07:11

Daniel Compton