I have seen much talk about predicate dispatch in Clojure lately and wonder if there is something to this thing. In other words, what is predicate dispatch and how does it differ from generic functions, OOP polymorphism, and patterns?
Thank you
I have seen much talk about predicate dispatch in Clojure lately and wonder if
Share
Edited: Clojure multimethods are not predicate dispatch.
In traditional object-oriented programming, polymorphism means that you can have multiple implementations of a method, and the exact implementation that gets called is determined by the type of the object on which you called the method. This is type dispatch.
Clojure multimethods extend this so that an arbitrary function can decide which implementation gets called. In the Clojure form
(defmulti name f), the functionfis the dispatch function.The dispatch function could be
class, in which case you’re back to type dispatch. But that function could be anything else: computing a dispatch value, looking up stuff in a database, even calling out to a web service.True predicate dispatch would potentially allow each method implementation to specify one or more dispatch functions (predicates) to decide when that method applies. This is more general than multimethods but more complicated to implement. Clojure does not support it.
Generic function is a term from other Lisps. Common Lisp, for example, provides generic functions which can dispatch on type plus a restricted set of other functions.