Using protocols

Protocols provide polymorphism between different data types. Defining protocols are defining Java's interfaces in fact. We can define protocols for Clojure's records and types. It is also possible to define protocols for Java classes.

Getting ready

This section does not make use of any external library, so you can just start a REPL and be ready.

How to do it...

Let's start with defining records and types, and then look at how to define and use protocols.

Defining a record

Here, we will define a record of the author. We have shown how to express the author Sir Conan Doyle using a map in Chapter 2, Interacting with Collections:

{:name "Arthur Ignatius Conan Doyle" :born "22-May-1859" :died "7-July-1930" :occupation ["novelist" "short story ...

Get Clojure Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.