Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Clojure Polymorphism

You're reading from   Clojure Polymorphism Leverage Clojure's polymorphic tools to develop your applications

Arrow left icon
Product type Paperback
Published in Nov 2019
Publisher
ISBN-13 9781838982362
Length 56 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Paul Stadig Paul Stadig
Author Profile Icon Paul Stadig
Paul Stadig
Arrow right icon
View More author details
Toc

Semantic Gotcha

One of the selling points of protocols is that they are namespaced functions. This mitigates some of the issues of "monkey patching" where someone adds a new function definition to an existing class. In languages where a class is also a namespace, you can run into conflicts if two definitions of a function named get are added to a class at the same time. Since Clojure's protocol functions are namespaced, each person can define their own my.company.project/get and open.source.project/get functions.

However, there is a gotcha. Recall that a protocol also generates a Java interface, and when you extend a protocol in a defrecord, the generated class will implement the protocol's Java interface. Suppose that we defined two protocols:

1 (ns stadig.dog)

2

3 (defprotocol IDog

4   (emote [this]))

1 (ns stadig.cat)

2

3 (defprotocol ICat

4   (emote [this]))

The following code listings do different things:

1 (defrecord CatDog...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime