January 2017
Intermediate to advanced
565 pages
13h 46m
English
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 ...
Read now
Unlock full access