Polymorphism
I would like to examine the theme of polymorphism. Polymorphism is a property of a function. When a function is polymorphic, its behavior will depend on the arguments you give to it. The most common type of polymorphism is type-based. In a language like Java, you call methods on objects. The object on which you call a method is an implicit first argument to the method, and the code that runs will depend on the type of that object.
The decision about which code to run is called dispatch. Aside from type-based dispatch on the first argument, dispatch could also consider the number of arguments, the type of each of the arguments, and so on.
Clojure gives you a few options for polymorphic dispatch:
- defmulti: A multimethod is the most general type of dispatch. You can decide how the dispatch will be performed by providing a dispatch function, and this dispatch function can run arbitrary Clojure code.
- defprotocol: A protocol function is a very specific (but common...