Decentralized Polymorphism

One interesting—and useful—aspect of protocols is that you can define and implement them after the fact. Imagine, for example, that you are working on a Clojure application and six months ago one of your colleagues implemented the Employee, FictionalCharacter, and SuperComputer records. And now, for reasons that need not concern us, you have just been handed the job of producing a positive marketing slogan for instances of each of these record types. So you start by defining a new protocol:

 (defprotocol Marketable
  (make-slogan [this]))

And then you go back and modify the Employee, FictionalCharacter, and SuperComputer defrecords so that they support the new protocol. Well, you could do that, but you don’t have to. ...

Get Getting Clojure 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.