Skip to content

Commit df28345

Browse files
committed
Fix another variant of print-method conflict
See code comment for rationale. Fixes #255.
1 parent 37faf9e commit df28345

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

src/manifold/deferred.clj

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,24 +1522,40 @@
15221522
body))
15231523

15241524

1525-
1526-
(defmethod print-method IDeferred [o ^Writer w]
1525+
(defn- print-deferred [d ^Writer w]
15271526
(.write w
15281527
(str
1529-
"<< "
1530-
(if (realized? o)
1531-
(try
1532-
(let [x @o]
1533-
(pr-str x))
1534-
(catch Throwable e
1535-
(str "ERROR: " (pr-str e))))
1536-
"\u2026")
1537-
" >>")))
1528+
"<< "
1529+
(if (realized? d)
1530+
(try
1531+
(let [x @d]
1532+
(pr-str x))
1533+
(catch Throwable e
1534+
(str "ERROR: " (pr-str e))))
1535+
"\u2026")
1536+
" >>")))
1537+
1538+
(defmethod print-method IDeferred [o w]
1539+
(print-deferred o w))
1540+
1541+
;; Implement `print-method` for all concrete types, too, to be robust against third parties
1542+
;; providing conflicting implementations for any of the interfaces. The alternative of using
1543+
;; `prefer-method` would require us to also define preferences between *all* combinations of
1544+
;; interfaces (see https://clojure.atlassian.net/browse/CLJ-396). Not only is this very laborious,
1545+
;; it's also overstepping the scope of a library since we'd also have to do this for conflicts
1546+
;; between external interfaces (e.g. `IDeref` and `CompletionStage`).
1547+
1548+
(defmethod print-method Deferred [o w]
1549+
(print-deferred o w))
15381550

1539-
(prefer-method print-method IDeferred IDeref)
1540-
(prefer-method print-method IDeferred CompletionStage)
1551+
(defmethod print-method LeakAwareDeferred [o w]
1552+
(print-deferred o w))
15411553

1554+
(defmethod print-method SuccessDeferred [o w]
1555+
(print-deferred o w))
15421556

1557+
(defmethod print-method ErrorDeferred [o w]
1558+
(print-deferred o w))
15431559

15441560
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
15451561
;; CompletionStage helper fns

0 commit comments

Comments
 (0)