2017-03-21 52 views
1

定制打印方法要自定义记录打印,我通常添加的打印方法:增加对clojurescript

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

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

,但是当我在clojurescript我,我得到一个错误:

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

我该怎么做cljs?

回答

2

我认为在ClojureScript中这样做的方法是将IPrintWithWriter协议扩展到您的对象,例如,

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

我找不到这么多的官方文档,所以可能有另一种/更好的方法来做到这一点。

+0

这个工作真的很好。谢谢! – zcaudate