O'Reilly logo

Clojure Programming Cookbook by Nicolas Modrzyk, Makoto Hashimoto

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required