|
| 1 | += Understanding Clojure's Polymorphism |
| 2 | +Ikuru Kanuma |
| 3 | +2017-07-20 |
| 4 | +:type: guides |
| 5 | +:toc: macro |
| 6 | +:icons: font |
| 7 | + |
| 8 | +ifdef::env-github,env-browser[:outfilesuffix: .adoc] |
| 9 | + |
| 10 | +== Goals of this guide |
| 11 | + |
| 12 | +Clojue supports several constructs for speaking to the Java world |
| 13 | +and creating types for polymorphic dispatch. + |
| 14 | +Because these constructs have overlapping capabilities, it may be confusing to know which construct to use at a given situation. + |
| 15 | +Hopefully this guide clarifies what each construct is good at, while presenting minimal usage examples. |
| 16 | + |
| 17 | +== Warm up with some Java |
| 18 | + |
| 19 | +Let's warm up with some Java interop: |
| 20 | + |
| 21 | +[source,clojure-repl] |
| 22 | +---- |
| 23 | +user=> (import 'java.util.Date) |
| 24 | +java.util.Date |
| 25 | +user=> (.toString (Date.)) |
| 26 | +"Fri Jul 21 11:40:49 JST 2017" |
| 27 | +---- |
| 28 | + |
| 29 | +Java Interop works. Cool! |
| 30 | + |
| 31 | +== Proxy a Java class and/or Interfaces |
| 32 | + |
| 33 | +Say we want the .toString method to add a greeting at the beginning for friendlyness. + |
| 34 | +The proxy macro can be used to create an adhoc object that extends a Java Class: |
| 35 | + |
| 36 | +[source,clojure-repl] |
| 37 | +---- |
| 38 | +user=> (def px (proxy [Date] [] |
| 39 | + (toString [] |
| 40 | + (str "Hello there! It is now " |
| 41 | + (proxy-super toString))))) |
| 42 | +user=> (.toString px) |
| 43 | +"Hello there! It is now Fri Jul 21 11:48:14 JST 2017" |
| 44 | +---- |
| 45 | +The ad hoc object can also implement Java Interfaces: |
| 46 | + |
| 47 | +[source,clojure-repl] |
| 48 | +---- |
| 49 | +(import 'java.io.Closeable) |
| 50 | +(import 'java.util.concurrent.Callable) |
| 51 | +user=> (def px (proxy [Date Callable Closeable] [] |
| 52 | + (toString [] |
| 53 | + (str "Hello there! It is now " |
| 54 | + (proxy-super toString))) |
| 55 | + (call [] |
| 56 | + (prn "Someone called me!")) |
| 57 | + (close [] |
| 58 | + (prn "closing!")))) |
| 59 | +user=> (.close px) |
| 60 | +"closing!" |
| 61 | +nil |
| 62 | +user=> (.call px) |
| 63 | +"Someone called me!" |
| 64 | +nil |
| 65 | +---- |
| 66 | + |
| 67 | +== Leaving Java with defrecord |
| 68 | + |
| 69 | +Sofar this is all dealing with Java stuff from Clojure. + |
| 70 | +If we do not have to extend from a concrete Java Type, we can define our own types |
| 71 | +that implement interfaces (and protocols, coming up next!) from Clojure via the |
| 72 | +link:https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/defrecord[defrecord] macro: |
| 73 | + |
| 74 | +[source,clojure-repl] |
| 75 | +---- |
| 76 | +user=> (defrecord Foo [a b] |
| 77 | + Closeable |
| 78 | + (close [this] |
| 79 | + (prn (+ a b)))) |
| 80 | +user.Foo |
| 81 | +user=> (.close (Foo. 2 2)) |
| 82 | +4 |
| 83 | +nil |
| 84 | +---- |
| 85 | + |
| 86 | +Records are nicer for the reasons described in the https://clojure.org/reference/datatypes#_deftype_and_defrecord[reference]. |
| 87 | + |
| 88 | +https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/deftype[deftype] is |
| 89 | +also available for implementing lower level constructs that require mutatable fields. |
| 90 | + |
| 91 | +== Protocols; like Java Interfaces, but better |
| 92 | +https://clojure.org/reference/protocols[protocols] offer similar capabilities as Java interfaces, but is more powerfuld because: |
| 93 | + |
| 94 | +* It is a cross platform construct |
| 95 | +* It allows third party types to participate in any protocols |
| 96 | + |
| 97 | +Let's make a protocol that handles Java Date instances as well as Foo records: |
| 98 | + |
| 99 | +[source,clojure-repl] |
| 100 | +---- |
| 101 | +user=> (extend-protocol IBaz |
| 102 | + Date;;Thing from Java |
| 103 | + (baz [this] |
| 104 | + (str "baz method for a Date: " |
| 105 | + (.toString this))) |
| 106 | + Foo;;Clojure Record |
| 107 | + (baz [this] |
| 108 | + (str "baz method for a Foo record!"))) |
| 109 | +nil |
| 110 | +user=> (baz (Date.)) |
| 111 | +"baz method for a Date: Fri Jul 21 14:04:46 JST 2017" |
| 112 | +user=> (baz (Foo. 1 1)) |
| 113 | +"baz method for a Foo record!" |
| 114 | +---- |
| 115 | + |
| 116 | +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). + |
| 117 | +If we were to apply a custom abstraction for Java Dates with an Interface IBaz, |
| 118 | +we must: |
| 119 | + |
| 120 | +* Go to the original source code of java.util.Date and say it implements IBaz |
| 121 | +* Also add IBaz to the official jdk release |
| 122 | + |
| 123 | +Unlikely to happen, right? |
| 124 | + |
| 125 | +== Reify-ing Java Interfaces or Protocols |
| 126 | +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: |
| 127 | + |
| 128 | +[source,clojure-repl] |
| 129 | +---- |
| 130 | +user=> (def rf (reify |
| 131 | + Closeable |
| 132 | + (close [this] |
| 133 | + (prn "reified closing!!")) |
| 134 | + IBaz |
| 135 | + (baz [this] |
| 136 | + "reified baz"))) |
| 137 | +nil |
| 138 | +user=> (baz rf) |
| 139 | +"reified baz" |
| 140 | +user=> (.close rf) |
| 141 | +"reified closing!!" |
| 142 | +nil |
| 143 | +---- |
| 144 | + |
| 145 | +One might ask "Doesn't proxy achieves the same if you do not need to extend a concrete Type?" + |
| 146 | +The answer is reify has better performance. |
| 147 | + |
| 148 | +== Take away |
| 149 | +To wrap up, here are some rules of thumb: |
| 150 | + |
| 151 | +* Prefer protocols and records over Java Types; stay in Clojure |
| 152 | +* If you must extend a Java Class, use proxy |
| 153 | +* If you want a on-off implementation of a Protocol/Interface, use reify |
0 commit comments