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 []
    (list 'do
          (list 'ns (gensym))
          '(do (definterface A)
               (definterface B)

               (defprotocol P
                 (a [this]))

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

  (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.