From d5e3fe1c25a4431083a459f0c43755433e0f98ed Mon Sep 17 00:00:00 2001 From: iku000888 Date: Thu, 20 Jul 2017 18:05:59 +0900 Subject: [PATCH 1/9] Guide for clojure's datatype constructs --- content/guides/clj_datatype_constructs.adoc | 153 ++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 content/guides/clj_datatype_constructs.adoc diff --git a/content/guides/clj_datatype_constructs.adoc b/content/guides/clj_datatype_constructs.adoc new file mode 100644 index 00000000..6afcd1d1 --- /dev/null +++ b/content/guides/clj_datatype_constructs.adoc @@ -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 + +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 \ No newline at end of file From 3217cb3777b0f4c315671dec56ef3e0e3e9bc5a0 Mon Sep 17 00:00:00 2001 From: Erik Assum Date: Tue, 1 Aug 2017 14:29:46 +0200 Subject: [PATCH 2/9] Fix some spelling erors --- content/guides/clj_datatype_constructs.adoc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/content/guides/clj_datatype_constructs.adoc b/content/guides/clj_datatype_constructs.adoc index 6afcd1d1..67218da1 100644 --- a/content/guides/clj_datatype_constructs.adoc +++ b/content/guides/clj_datatype_constructs.adoc @@ -9,7 +9,7 @@ ifdef::env-github,env-browser[:outfilesuffix: .adoc] == Goals of this guide -Clojue supports several constructs for speaking to the Java world +Clojure 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. @@ -142,7 +142,7 @@ user=> (.close rf) nil ---- -One might ask "Doesn't proxy achieves the same if you do not need to extend a concrete Type?" + +One might ask "Doesn't proxy achieve the same if you do not need to extend a concrete Type?" + The answer is reify has better performance. == Take away @@ -150,4 +150,4 @@ 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 \ No newline at end of file +* If you want a on-off implementation of a Protocol/Interface, use reify From c8cb5afba3d24a3c0456e8ef8c66907e400d7c97 Mon Sep 17 00:00:00 2001 From: Ikuru K Date: Wed, 2 Aug 2017 23:57:21 +0900 Subject: [PATCH 3/9] Fix typo --- content/guides/clj_datatype_constructs.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/guides/clj_datatype_constructs.adoc b/content/guides/clj_datatype_constructs.adoc index 67218da1..fc0c137b 100644 --- a/content/guides/clj_datatype_constructs.adoc +++ b/content/guides/clj_datatype_constructs.adoc @@ -150,4 +150,4 @@ 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 +* If you want a one-off implementation of a Protocol/Interface, use reify From d5f86fe89b7c2f4d1bb4d9f119ce7d6530e0d722 Mon Sep 17 00:00:00 2001 From: iku000888 Date: Sat, 12 Aug 2017 04:16:15 +0900 Subject: [PATCH 4/9] Incorporate feedback --- content/guides/clj_datatype_constructs.adoc | 111 +++++++++++--------- 1 file changed, 60 insertions(+), 51 deletions(-) diff --git a/content/guides/clj_datatype_constructs.adoc b/content/guides/clj_datatype_constructs.adoc index fc0c137b..38fa551a 100644 --- a/content/guides/clj_datatype_constructs.adoc +++ b/content/guides/clj_datatype_constructs.adoc @@ -1,4 +1,4 @@ -= Understanding Clojure's Polymorphism += Understanding Clojure's Datatype Constructs Ikuru Kanuma 2017-07-20 :type: guides @@ -10,37 +10,38 @@ ifdef::env-github,env-browser[:outfilesuffix: .adoc] == Goals of this guide Clojure supports several constructs for speaking to the Java world -and creating types for polymorphic dispatch. + +and/or 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! +This guide clarifies what each construct is good at, while presenting minimal usage examples. == Proxy a Java class and/or Interfaces -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: +The proxy macro can be used to create an adhoc object that extends a Java Class. +The example below extends the good old java.util.ArrayList such that a Clojure vector +wrapped in an atom is used internally to manage state. [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" +(import 'java.util.ArrayList) + +(def px (let [atm (atom [])] + (proxy [ArrayList] [] + (add [e] + (swap! atm #(conj % e)) + true) + (get [idx] + (get @atm idx)) + (size [] (count @atm))))) + +(dotimes [n 10] + (.add px n)) +;; => nil +(.get px 0) +;; => 0 +(.get px 6) +;; => 6 +(.size px) +;; => 10 ---- The ad hoc object can also implement Java Interfaces: @@ -48,25 +49,31 @@ The ad hoc object can also implement Java Interfaces: ---- (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) + +(def px (let [atm (atom [])] + (proxy [ArrayList Closeable Callable] [] + (add [e] + (swap! atm #(conj % e)) + true) + (get [idx] + (get @atm idx)) + (size [] (count @atm)) + (call [] + (prn "Someone called me!")) + (close [] + (prn "closing!"))))) + +(.close px) "closing!" nil -user=> (.call px) +(.call px) "Someone called me!" nil ---- == Leaving Java with defrecord -Sofar this is all dealing with Java stuff from Clojure. + +So far 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: @@ -83,37 +90,39 @@ user=> (.close (Foo. 2 2)) nil ---- -Records are nicer for the reasons described in the https://clojure.org/reference/datatypes#_deftype_and_defrecord[reference]. +Records are nicer than Java classes 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: +https://clojure.org/reference/protocols[Protocols] offer similar capabilities as Java interfaces, but are more powerful because: -* It is a cross platform construct -* It allows third party types to participate in any protocols +* They are a cross platform construct +* They allow third party types to participate in any protocols -Let's make a protocol that handles Java Date instances as well as Foo records: +Let's make a protocol that handles Java ArrayList instances as well as Foo records: [source,clojure-repl] ---- +user=> (defprotocol IBaz + (baz [this])) + user=> (extend-protocol IBaz - Date;;Thing from Java + ArrayList ;;A Java Class (baz [this] - (str "baz method for a Date: " - (.toString this))) - Foo;;Clojure Record + "ArrayList Baz") + Foo ;;A Clojure Record (baz [this] - (str "baz method for a Foo record!"))) + "Foo Baz")) nil -user=> (baz (Date.)) -"baz method for a Date: Fri Jul 21 14:04:46 JST 2017" +user=> (baz (ArrayList.)) +"ArrayList Baz" user=> (baz (Foo. 1 1)) -"baz method for a Foo record!" +"Foo Baz" ---- -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). + +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: @@ -150,4 +159,4 @@ 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 one-off implementation of a Protocol/Interface, use reify +* If you want an anonymous implementation of a Protocol/Interface, use reify From d0a49783a0d72013f0ff39499377a082b8dbfdd0 Mon Sep 17 00:00:00 2001 From: Christian Westrom Date: Wed, 12 May 2021 22:10:09 -0400 Subject: [PATCH 5/9] Section on proxies is last in the article --- content/guides/clj_datatype_constructs.adoc | 111 ++++++++++---------- 1 file changed, 55 insertions(+), 56 deletions(-) diff --git a/content/guides/clj_datatype_constructs.adoc b/content/guides/clj_datatype_constructs.adoc index 38fa551a..dd3ad317 100644 --- a/content/guides/clj_datatype_constructs.adoc +++ b/content/guides/clj_datatype_constructs.adoc @@ -14,62 +14,6 @@ and/or 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. + This guide clarifies what each construct is good at, while presenting minimal usage examples. -== Proxy a Java class and/or Interfaces - -The proxy macro can be used to create an adhoc object that extends a Java Class. -The example below extends the good old java.util.ArrayList such that a Clojure vector -wrapped in an atom is used internally to manage state. - -[source,clojure-repl] ----- -(import 'java.util.ArrayList) - -(def px (let [atm (atom [])] - (proxy [ArrayList] [] - (add [e] - (swap! atm #(conj % e)) - true) - (get [idx] - (get @atm idx)) - (size [] (count @atm))))) - -(dotimes [n 10] - (.add px n)) -;; => nil -(.get px 0) -;; => 0 -(.get px 6) -;; => 6 -(.size px) -;; => 10 ----- -The ad hoc object can also implement Java Interfaces: - -[source,clojure-repl] ----- -(import 'java.io.Closeable) -(import 'java.util.concurrent.Callable) - -(def px (let [atm (atom [])] - (proxy [ArrayList Closeable Callable] [] - (add [e] - (swap! atm #(conj % e)) - true) - (get [idx] - (get @atm idx)) - (size [] (count @atm)) - (call [] - (prn "Someone called me!")) - (close [] - (prn "closing!"))))) - -(.close px) -"closing!" -nil -(.call px) -"Someone called me!" -nil ----- == Leaving Java with defrecord @@ -154,6 +98,61 @@ nil One might ask "Doesn't proxy achieve the same if you do not need to extend a concrete Type?" + The answer is reify has better performance. +== Proxy a Java class and/or Interfaces + +The proxy macro can be used to create an adhoc object that extends a Java Class. +The example below extends the good old java.util.ArrayList such that a Clojure vector +wrapped in an atom is used internally to manage state. + +[source,clojure-repl] +---- +(import 'java.util.ArrayList) + +(def px (let [atm (atom [])] + (proxy [ArrayList] [] + (add [e] + (swap! atm #(conj % e)) + true) + (get [idx] + (get @atm idx)) + (size [] (count @atm))))) + +(dotimes [n 10] + (.add px n)) +;; => nil +(.get px 0) +;; => 0 +(.get px 6) +;; => 6 +(.size px) +;; => 10 +---- +The ad hoc object can also implement Java Interfaces: + +[source,clojure-repl] +---- +(import 'java.io.Closeable) +(import 'java.util.concurrent.Callable) + +(def px (let [atm (atom [])] + (proxy [ArrayList Closeable Callable] [] + (add [e] + (swap! atm #(conj % e)) + true) + (get [idx] + (get @atm idx)) + (size [] (count @atm)) + (call [] + (prn "Someone called me!")) + (close [] + (prn "closing!"))))) + +(.close px) +"closing!" +nil +(.call px) +"Someone called me!" +---- == Take away To wrap up, here are some rules of thumb: From 2039b7cfcacb14e32974dc6608873ab01b362fe2 Mon Sep 17 00:00:00 2001 From: Christian Westrom Date: Wed, 12 May 2021 22:17:20 -0400 Subject: [PATCH 6/9] Capitalization, formatting, and grammar fixes. --- content/guides/clj_datatype_constructs.adoc | 26 ++++++++++----------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/content/guides/clj_datatype_constructs.adoc b/content/guides/clj_datatype_constructs.adoc index dd3ad317..a5a5eebc 100644 --- a/content/guides/clj_datatype_constructs.adoc +++ b/content/guides/clj_datatype_constructs.adoc @@ -45,7 +45,7 @@ https://clojure.org/reference/protocols[Protocols] offer similar capabilities as * They are a cross platform construct * They allow third party types to participate in any protocols -Let's make a protocol that handles Java ArrayList instances as well as Foo records: +Let's make a protocol that handles Java `ArrayList` instances as well as `Foo` records: [source,clojure-repl] ---- @@ -66,17 +66,17 @@ user=> (baz (Foo. 1 1)) "Foo Baz" ---- -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, +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 +* 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: +Sometimes we want to create things that implement a protocol/interface but do not want to give them 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] ---- @@ -95,13 +95,13 @@ user=> (.close rf) nil ---- -One might ask "Doesn't proxy achieve the same if you do not need to extend a concrete Type?" + +One might ask "Doesn't proxy achieve the same if you do not need to extend a concrete type?" + The answer is reify has better performance. == Proxy a Java class and/or Interfaces -The proxy macro can be used to create an adhoc object that extends a Java Class. -The example below extends the good old java.util.ArrayList such that a Clojure vector +The proxy macro can be used to create an adhoc object that extends a Java class. +The example below extends `java.util.ArrayList` such that a Clojure vector wrapped in an atom is used internally to manage state. [source,clojure-repl] @@ -127,7 +127,7 @@ wrapped in an atom is used internally to manage state. (.size px) ;; => 10 ---- -The ad hoc object can also implement Java Interfaces: +The ad hoc object can also implement Java interfaces: [source,clojure-repl] ---- @@ -156,6 +156,6 @@ nil == 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 an anonymous implementation of a Protocol/Interface, use reify +* Prefer protocols and records over Java types; stay in Clojure +* If you must extend a Java class, use proxy +* If you want an anonymous implementation of a protocol/interface, use reify From 13b0e4924ae17c680b5855afbe82201ded9234e5 Mon Sep 17 00:00:00 2001 From: Christian Westrom Date: Thu, 13 May 2021 00:37:24 -0400 Subject: [PATCH 7/9] Made some nicer examples. --- content/guides/clj_datatype_constructs.adoc | 85 +++++++++++---------- 1 file changed, 44 insertions(+), 41 deletions(-) diff --git a/content/guides/clj_datatype_constructs.adoc b/content/guides/clj_datatype_constructs.adoc index a5a5eebc..e2beec9e 100644 --- a/content/guides/clj_datatype_constructs.adoc +++ b/content/guides/clj_datatype_constructs.adoc @@ -11,59 +11,64 @@ ifdef::env-github,env-browser[:outfilesuffix: .adoc] Clojure supports several constructs for speaking to the Java world and/or 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. + +Because these constructs have overlapping capabilities, +it may be confusing to know which construct to use at a given situation. + + This guide clarifies what each construct is good at, while presenting minimal usage examples. == Leaving Java with defrecord -So far 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 +user=> (defrecord Person [first-name last-name age drunk]) +user.Person +user=> (def piklrik (Person. "Pickle" "Rick" :unknown true)) +#'user.meeseeks ---- -Records are nicer than Java classes for the reasons described in the https://clojure.org/reference/datatypes#_deftype_and_defrecord[reference]. +Records are nicer than Java classes for a few reasons: + +* TODO: add more nicities. +* They provide a complete implementation of a persistent map. That means that all values can be accessed like a map. + +[source,clojure-repl] +---- +user=> (:first-name piklrik) +-> "Pickle" +user=> (:last-name piklrik) +-> "Rick" +---- + +The https://clojure.org/reference/datatypes#_deftype_and_defrecord[reference] describes the features of records in more detail. 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. +also available for implementing lower level constructs that require mutatable fields +or don't have map semantics. -== Protocols; like Java Interfaces, but better +== Protocols; They're Like Java Interfaces https://clojure.org/reference/protocols[Protocols] offer similar capabilities as Java interfaces, but are more powerful because: * They are a cross platform construct -* They allow third party types to participate in any protocols +* They allow third party types to participate in any protocol -Let's make a protocol that handles Java `ArrayList` instances as well as `Foo` records: +Let's make a protocol that handles instances of `Person`: [source,clojure-repl] ---- -user=> (defprotocol IBaz - (baz [this])) - -user=> (extend-protocol IBaz - ArrayList ;;A Java Class - (baz [this] - "ArrayList Baz") - Foo ;;A Clojure Record - (baz [this] - "Foo Baz")) +user=> (defprotocol Introduction + (introduce [this] "This is a docstring, not a method.")) +Introduction +user=> (extend-protocol Introduction + Person + (introduce [p] (str "I'm " (:first-name p) " " (:last-name p) "!!"))) nil -user=> (baz (ArrayList.)) -"ArrayList Baz" -user=> (baz (Foo. 1 1)) -"Foo Baz" +user=> (introduce piklrik) +"I'm Pickle Rick!!" ---- 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`). + @@ -80,19 +85,17 @@ Sometimes we want to create things that implement a protocol/interface but do no [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!!" +user=> (defn make-meeseeks + [p] + (reify Introduction + (introduce [_] + (str "I'm " (:first-name p) " " (:last-name p) ", look at me!")))) nil +user=> (def meeseeks + (make-meeseeks (Person. "Mr." "Meeseeks" 0 false))) +#'user/meeseeks +user=> (introduce meeseeks) +"I'm Mr. Meeseeks, look at me!" ---- One might ask "Doesn't proxy achieve the same if you do not need to extend a concrete type?" + From 3bd20c7deffb0a82b5b8d3cc02c58a5698f843ed Mon Sep 17 00:00:00 2001 From: Christian Westrom Date: Thu, 13 May 2021 00:42:58 -0400 Subject: [PATCH 8/9] Minor edits to repl examples --- content/guides/clj_datatype_constructs.adoc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/content/guides/clj_datatype_constructs.adoc b/content/guides/clj_datatype_constructs.adoc index e2beec9e..33ecf4cb 100644 --- a/content/guides/clj_datatype_constructs.adoc +++ b/content/guides/clj_datatype_constructs.adoc @@ -28,7 +28,7 @@ link:https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/defrec user=> (defrecord Person [first-name last-name age drunk]) user.Person user=> (def piklrik (Person. "Pickle" "Rick" :unknown true)) -#'user.meeseeks +#'user.piklrik ---- Records are nicer than Java classes for a few reasons: @@ -39,9 +39,9 @@ Records are nicer than Java classes for a few reasons: [source,clojure-repl] ---- user=> (:first-name piklrik) --> "Pickle" +"Pickle" user=> (:last-name piklrik) --> "Rick" +"Rick" ---- The https://clojure.org/reference/datatypes#_deftype_and_defrecord[reference] describes the features of records in more detail. @@ -85,14 +85,14 @@ Sometimes we want to create things that implement a protocol/interface but do no [source,clojure-repl] ---- -user=> (defn make-meeseeks +user=> (defn spawn-meeseeks [p] (reify Introduction (introduce [_] (str "I'm " (:first-name p) " " (:last-name p) ", look at me!")))) -nil +#'user/make-meeseeks user=> (def meeseeks - (make-meeseeks (Person. "Mr." "Meeseeks" 0 false))) + (spawn-meeseeks (Person. "Mr." "Meeseeks" 0 false))) #'user/meeseeks user=> (introduce meeseeks) "I'm Mr. Meeseeks, look at me!" From 8717d7073d65047504beeeec5d7919075769b63a Mon Sep 17 00:00:00 2001 From: Christian Westrom Date: Thu, 13 May 2021 01:33:14 -0400 Subject: [PATCH 9/9] Changed the order of the takeaways. --- content/guides/clj_datatype_constructs.adoc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/content/guides/clj_datatype_constructs.adoc b/content/guides/clj_datatype_constructs.adoc index 33ecf4cb..477f4595 100644 --- a/content/guides/clj_datatype_constructs.adoc +++ b/content/guides/clj_datatype_constructs.adoc @@ -159,6 +159,7 @@ nil == 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 +TODO: Add more rules of thumb. I find them very helpful. +* Prefer protocols and records over Java types; Stay in Clojure * If you want an anonymous implementation of a protocol/interface, use reify +* If you must extend a Java class, use proxy