2017-09-08 46 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>