2017-09-08 41 views
1

我有一个应用程序有很多大地图和其他东西,打印时很笨拙的读取,所以我为它们做了一个自定义打印功能,并设置print-method来调用它,就像这样:如何绕过打印方法

(defmethod print-method clojure.lang.PersistentArrayMap [v ^java.io.Writer w] 
    (.write w (fstr1 v))) 

里面fstr1,我怎么能叫普通的印刷方法,如果我确定该地图是不是需要特别处理的种吗?

This answer建议在元数据中放置一个:type,因为print-method将在该元数据上发送。我已经取得了一些成功,但我不能总是控制元数据,所以我希望有一种方法可以在fstr1之内“转发”到先前定义的打印方法。


供参考,这是我目前执行的fstr1

(defn fstr1 ^String [x] 
    (cond 
    (ubergraph? x) 
     (fstr-ubergraph x) 
    (map? x) 
     (case (:type x) 
     :move (fstr-move x) 
     :workspace "(a workspace)" 
     :bdx (fstr-bdx x) 
     :rxn (fstr-rxn x) 
     (apply str (strip-type x))) 
    :else 
     (apply str (strip-type x)))) 
+2

前调查替代解决方案 - 你试过了吗?(set!* print-length * 10)' – birdspider

回答

2

你总是可以重新绑定print-object抱膝真正print-object远,所以你可以在适当的时候把它叫做:

user> (let [real-print-method print-method] 
     (with-redefs [print-method (fn [v w] 
            (if (and (map? v) 
               (:foo v)) 
             (do 
             (real-print-method "{:foo " w) 
             (real-print-method (:foo v) w) 
             (real-print-method " ...}" w)) 
             (real-print-method v w)))] 
      (println {:foo 42 :bar 23} {:baz 11 :quux 0}))) 
{:foo 42 ...} {:baz 11, :quux 0} 
nil 
user>