Chapter 9
Multimethods

Clojure multimethods provide a flexible way to associate a function with a set of inputs. This is similar to Java polymorphism but more general. When you call a Java method, Java selects a specific implementation to execute by examining the type of a single object. When you call a Clojure multimethod, Clojure selects a specific implementation to execute by examining the result of any function you choose, applied to all the function’s arguments.

In this chapter, you’ll develop a thirst for multimethods by first living without them. Then you’ll build an increasingly complex series of multimethod implementations—first using multimethods to simulate polymorphism and then using multimethods to implement various ad hoc taxonomies.

Multimethods in Clojure are used much less often than polymorphism in object-oriented languages. But where they are used, they’re often the key feature in the code. We’ll close the chapter by looking at how multimethods are used in several open source Clojure projects and offer guidelines for when to use them in your own programs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.37.20