You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Records are nicer for the reasons described in the https://clojure.org/reference/datatypes#_deftype_and_defrecord[reference].
93
+
Records are nicer than Java classes for the reasons described in the https://clojure.org/reference/datatypes#_deftype_and_defrecord[reference].
87
94
88
95
https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/deftype[deftype] is
89
96
also available for implementing lower level constructs that require mutatable fields.
90
97
91
98
== Protocols; like Java Interfaces, but better
92
-
https://clojure.org/reference/protocols[protocols] offer similar capabilities as Java interfaces, but is more powerfuld because:
99
+
https://clojure.org/reference/protocols[Protocols] offer similar capabilities as Java interfaces, but are more powerful because:
93
100
94
-
* It is a cross platform construct
95
-
* It allows third party types to participate in any protocols
101
+
* They are a cross platform construct
102
+
* They allow third party types to participate in any protocols
96
103
97
-
Let's make a protocol that handles Java Date instances as well as Foo records:
104
+
Let's make a protocol that handles Java ArrayList instances as well as Foo records:
98
105
99
106
[source,clojure-repl]
100
107
----
108
+
user=> (defprotocol IBaz
109
+
(baz [this]))
110
+
101
111
user=> (extend-protocol IBaz
102
-
Date;;Thing from Java
112
+
ArrayList ;;A Java Class
103
113
(baz [this]
104
-
(str "baz method for a Date: "
105
-
(.toString this)))
106
-
Foo;;Clojure Record
114
+
"ArrayList Baz")
115
+
Foo ;;A Clojure Record
107
116
(baz [this]
108
-
(str "baz method for a Foo record!")))
117
+
"Foo Baz"))
109
118
nil
110
-
user=> (baz (Date.))
111
-
"baz method for a Date: Fri Jul 21 14:04:46 JST 2017"
119
+
user=> (baz (ArrayList.))
120
+
"ArrayList Baz"
112
121
user=> (baz (Foo. 1 1))
113
-
"baz method for a Foo record!"
122
+
"Foo Bax"
114
123
----
115
124
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). +
125
+
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
126
If we were to apply a custom abstraction for Java Dates with an Interface IBaz,
118
127
we must:
119
128
@@ -150,4 +159,4 @@ To wrap up, here are some rules of thumb:
150
159
151
160
* Prefer protocols and records over Java Types; stay in Clojure
152
161
* If you must extend a Java Class, use proxy
153
-
* If you want a one-off implementation of a Protocol/Interface, use reify
162
+
* If you want an anonymous implementation of a Protocol/Interface, use reify
0 commit comments