-
Notifications
You must be signed in to change notification settings - Fork 271
Guide for clojure's datatype constructs #202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
d5e3fe1
3217cb3
d647ac7
c8cb5af
d5f86fe
d0a4978
2039b7c
13b0e49
3bd20c7
8717d70
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
= Understanding Clojure's Polymorphism | ||
Ikuru Kanuma | ||
2017-07-20 | ||
:type: guides | ||
:toc: macro | ||
:icons: font | ||
|
||
ifdef::env-github,env-browser[:outfilesuffix: .adoc] | ||
|
||
== Goals of this guide | ||
|
||
Clojue supports several constructs for speaking to the Java world | ||
|
||
and creating types for polymorphic dispatch. + | ||
Because these constructs have overlapping capabilities, it may be confusing to know which construct to use at a given situation. + | ||
Hopefully this guide clarifies what each construct is good at, while presenting minimal usage examples. | ||
|
||
|
||
== Warm up with some Java | ||
|
||
|
||
Let's warm up with some Java interop: | ||
|
||
[source,clojure-repl] | ||
---- | ||
user=> (import 'java.util.Date) | ||
java.util.Date | ||
user=> (.toString (Date.)) | ||
"Fri Jul 21 11:40:49 JST 2017" | ||
---- | ||
|
||
Java Interop works. Cool! | ||
|
||
== Proxy a Java class and/or Interfaces | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Proxies should probably be the least frequently used so I don't like starting this guide with it. Should move to the end. |
||
|
||
Say we want the .toString method to add a greeting at the beginning for friendlyness. + | ||
The proxy macro can be used to create an adhoc object that extends a Java Class: | ||
|
||
|
||
[source,clojure-repl] | ||
---- | ||
user=> (def px (proxy [Date] [] | ||
(toString [] | ||
(str "Hello there! It is now " | ||
(proxy-super toString))))) | ||
user=> (.toString px) | ||
"Hello there! It is now Fri Jul 21 11:48:14 JST 2017" | ||
---- | ||
The ad hoc object can also implement Java Interfaces: | ||
|
||
|
||
[source,clojure-repl] | ||
---- | ||
(import 'java.io.Closeable) | ||
(import 'java.util.concurrent.Callable) | ||
user=> (def px (proxy [Date Callable Closeable] [] | ||
(toString [] | ||
(str "Hello there! It is now " | ||
(proxy-super toString))) | ||
(call [] | ||
(prn "Someone called me!")) | ||
(close [] | ||
(prn "closing!")))) | ||
user=> (.close px) | ||
"closing!" | ||
nil | ||
user=> (.call px) | ||
"Someone called me!" | ||
nil | ||
---- | ||
|
||
== Leaving Java with defrecord | ||
|
||
|
||
Sofar this is all dealing with Java stuff from Clojure. + | ||
|
||
If we do not have to extend from a concrete Java Type, we can define our own types | ||
that implement interfaces (and protocols, coming up next!) from Clojure via the | ||
link:https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/defrecord[defrecord] macro: | ||
|
||
[source,clojure-repl] | ||
---- | ||
user=> (defrecord Foo [a b] | ||
|
||
Closeable | ||
(close [this] | ||
(prn (+ a b)))) | ||
user.Foo | ||
user=> (.close (Foo. 2 2)) | ||
4 | ||
nil | ||
---- | ||
|
||
Records are nicer for the reasons described in the https://clojure.org/reference/datatypes#_deftype_and_defrecord[reference]. | ||
|
||
|
||
https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/deftype[deftype] is | ||
also available for implementing lower level constructs that require mutatable fields. | ||
|
||
|
||
== Protocols; like Java Interfaces, but better | ||
|
||
https://clojure.org/reference/protocols[protocols] offer similar capabilities as Java interfaces, but is more powerfuld because: | ||
|
||
|
||
* It is a cross platform construct | ||
|
||
* It allows third party types to participate in any protocols | ||
|
||
Let's make a protocol that handles Java Date instances as well as Foo records: | ||
|
||
[source,clojure-repl] | ||
---- | ||
user=> (extend-protocol IBaz | ||
Date;;Thing from Java | ||
|
||
(baz [this] | ||
(str "baz method for a Date: " | ||
(.toString this))) | ||
Foo;;Clojure Record | ||
|
||
(baz [this] | ||
(str "baz method for a Foo record!"))) | ||
nil | ||
user=> (baz (Date.)) | ||
"baz method for a Date: Fri Jul 21 14:04:46 JST 2017" | ||
user=> (baz (Foo. 1 1)) | ||
"baz method for a Foo record!" | ||
---- | ||
|
||
The main thing to realize here is that protocols are more powerful than Interfaces because we are able to create custom abstraction for Types that we do not control (e.g. java.util.Date). + | ||
|
||
If we were to apply a custom abstraction for Java Dates with an Interface IBaz, | ||
we must: | ||
|
||
* Go to the original source code of java.util.Date and say it implements IBaz | ||
* Also add IBaz to the official jdk release | ||
|
||
|
||
Unlikely to happen, right? | ||
|
||
== Reify-ing Java Interfaces or Protocols | ||
Sometimes we want to create things that implement a Protocol/Interface but do not want to give it a name for each of them. link:https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/reify[reify] does exactly that: | ||
|
||
|
||
[source,clojure-repl] | ||
---- | ||
user=> (def rf (reify | ||
Closeable | ||
(close [this] | ||
(prn "reified closing!!")) | ||
IBaz | ||
(baz [this] | ||
"reified baz"))) | ||
nil | ||
user=> (baz rf) | ||
"reified baz" | ||
user=> (.close rf) | ||
"reified closing!!" | ||
nil | ||
---- | ||
|
||
One might ask "Doesn't proxy achieves the same if you do not need to extend a concrete Type?" + | ||
The answer is reify has better performance. | ||
|
||
== Take away | ||
To wrap up, here are some rules of thumb: | ||
|
||
* Prefer protocols and records over Java Types; stay in Clojure | ||
|
||
* If you must extend a Java Class, use proxy | ||
|
||
* If you want a on-off implementation of a Protocol/Interface, use reify | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There seems to be confusion between the URL, the title, and other places about whether this is about datatypes, polymorphism, or both. Polymorphism to me includes multimethods. A discussion of datatypes should cover include defrecord and deftype. This page misses both, so it's not clear to me what the goal is here. Seems like clarifying this first would help.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
multimethods are out of scope of this guide, so not about polymorphism.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated title.
I do mention deftype, but not in depth believing that by default, one should reach for records.
Feedback would be appreciated though.