Welcome! Please see the About page for a little more info on how this works.

+5 votes
in Protocols by

If an object is an instance of multiple (unrelated) interfaces that each extend a protocol, the dispatched method will be chosen at random.

(defn reproduce []
  (eval 
    (list 'do
          (list 'ns (gensym))
          '(do (definterface A)
               (definterface B)

               (defprotocol P
                 (a [this]))

               (extend-protocol P
                 A
                 (a [this] :a)
                 B
                 (a [this] :b))
               (a (reify A B))))))

(frequencies
  (repeatedly 100 reproduce))
;=> {:b 52, :a 48}

One solution is to sort interfaces by name before choosing the implementation, so at least the nondeterminism is eliminated. We could go further and also print a warning in this case.

Logged as: https://clojure.atlassian.net/browse/CLJ-2656

Please log in or register to answer this question.

...