Chapter 9. Protocols, records, and types
This chapter covers
- An overview of the expression problem
- A custom solution to the expression problem
- Clojure’s solution to the expression problem
- Working with types and records: deftype, defrecord, and reify
Abstraction is an important tenet of software development because it results in code that’s more maintainable and extensible. Clojure itself is built on abstractions. For instance, most things in Clojure are coded to interfaces rather than being concrete implementations. This allows for reuse of code that expects those interfaces and allows for the seamless addition of more implementations to the mix.
Sooner or later, during your time on most projects, you’ll run into an abstraction-oriented issue known as the expression problem. It has to do with how to cleanly extend or use existing code—either something you wrote yourself or, more important, something you don’t own. In this chapter, we’ll explore the expression problem in some depth, as well as Clojure’s approach to handling it (protocols). Then, we’ll come up with a solution. Finally, we’ll explore the Clojure solution in more depth by looking at protocols, data types, and the reify macro.