Multimethods are just one of the options for polymorphism you have in Clojure, there are other ways to implement polymorphic functions.
Protocols are a little easier to understand and they might feel more similar to Java interfaces.
Lets try to define our shape program using protocols:
(defprotocol Shape "This is a protocol for shapes" (perimeter [this] "Calculates the perimeter of this shape") (area [this] "Calculates the area of this shape"))
Here, we have defined a protocol and it is called shaped and everything that implements this protocol must implement the following two functions: perimeter
and area
.
There are a number of ways to implement a protocol; one interesting feature is that you can even extend Java classes to implement a protocol without an access to the Java source and without having to recompile anything.
Let's start by creating a record that implements the type.