O'Reilly logo

Clojure for Domain-specific Languages by Kelker Ryan

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

Overriding methods with reify and proxy

Overriding methods of a protocol can be implemented with the reify form, while overrides for interfaces and classes can be implemented with the proxy form. These two forms are meant to provide a way to make anonymous instances of protocols and interfaces, but these forms double as a simple way to write method overrides.

Working with reify

We'll use the PCan protocol from the last section in the following examples. The following is the PCan protocol, in case you don't have it from the last section:

user> (defprotocol PCan
        (can-size [_])
        (open-can [_])
        (label [_]))
PCan

In the following example, reify is used to make a locally-scoped instance of the PCan class. Because the returned class can be requested by its ...

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