Share your thoughts in the 2021 Clojure Community Survey!

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

0 votes
in Clojure by

While the hierarchies produced with 'derive' allow multiple parents per child, there is no way to indicate precedence between those parents, other than by laboriously specifying 'prefer-method' for every type X every multimethod. When 2 multimethods are both applicable to the supplied arguments, Clojure produces a nonspecific IllegalArgumentException containing only an error string. All this means that while Clojure does have an "inheritance" mechanism in the form of the ad hoc hierarchies, it is currently not really possible to implement multiple inheritance using the ad hoc hierarchy mechanism. 'Prefer-method' will not scale up to use in large applications with complex type hierarchies and heavy use of multimethods.

Some potential ways to solve this are:
- allowing 'defmulti' to take a 'tie-breaker' function (tie-breaker (link: arglist speclist1 speclist2) ...) which is called instead of throwing an IllegalArgumentException, and must return the 'winning speclist'.
- instead of throwing IllegalArgumentException, throw a TiedMultiMethodsException -- the exception instance should contain the offending speclists, the function, and the arguments that were supplied.
- allowing specification of precedence when using 'derive' (if only via a "last in = highest precedence" rule).

Paul

2 Answers

0 votes
by

Comment made by: importer

Converted from http://www.assembla.com/spaces/clojure/tickets/396

0 votes
by
Reference: https://clojure.atlassian.net/browse/CLJ-396 (reported by alex+import)
...