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

Polymorphism

I would like to examine the theme of polymorphism. Polymorphism is a property of a function. When a function is polymorphic, its behavior will depend on the arguments you give to it. The most common type of polymorphism is type-based. In a language like Java, you call methods on objects. The object on which you call a method is an implicit first argument to the method, and the code that runs will depend on the type of that object.

The decision about which code to run is called dispatch. Aside from type-based dispatch on the first argument, dispatch could also consider the number of arguments, the type of each of the arguments, and so on.

Clojure gives you a few options for polymorphic dispatch:

  • defmulti: A multimethod is the most general type of dispatch. You can decide how the dispatch will be performed by providing a dispatch function, and this dispatch function can run arbitrary Clojure code.
  • defprotocol: A protocol function is a very specific (but common...
You have been reading a chapter from
Clojure Polymorphism
Published in: Nov 2019
Publisher:
ISBN-13: 9781838982362
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 $19.99/month. Cancel anytime