Skip to content
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

Guide for clojure's datatype constructs #202

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
153 changes: 153 additions & 0 deletions content/guides/clj_datatype_constructs.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
= Understanding Clojure's Polymorphism
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Clojue" typo. Why is "speaking to the Java world" relevant here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important because the proxy macro was created first specifically for speaking to the Java world via extending classes and implementing interfaces.

Later came protocols/reify/deftype/defrecord which are not created for Java interop, but instead are Clojure's own constructs for polymorphic dispatch.

As a newbie the overlap of proxy and reify were confusing until I learned the above distinction.

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strike "Hopefully" - no reason to be apologetic - make it good and improve it if it's missing something.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point :)


== Warm up with some Java
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any reason to include this section, it doesn't seem relevant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropped


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
Copy link
Member

Choose a reason for hiding this comment

The 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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a weak example. It would be better to pick a concrete class that you need to extend and provide a method that's abstract in the super class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up extending an ArrayList, because it has a SuperClass AbstractList and I wanted the example to be consise as posible.

Feedback on improvement would be appreciated.


[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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interfaces should not be capitalized here.


[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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this called "Leaving Java"?


Sofar this is all dealing with Java stuff from Clojure. +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Sofar" typo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

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]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this a real example?

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].
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nicer than what?

Copy link
Contributor Author

@iku000888 iku000888 Aug 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicer than Java Classes(fixed as such)


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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... or don't have map semantics


== Protocols; like Java Interfaces, but better
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need to editorialize in the header

https://clojure.org/reference/protocols[protocols] offer similar capabilities as Java interfaces, but is more powerfuld because:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix capitalization at beginning of sentence ("protocols") and "is" should be "are" to match the plural "protocols". Also, typo "powerfuld".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


* It is a cross platform construct
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think "It is" should be "They are" here to match plural "protocols".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

* 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the lack of spaces before ;; make "Date;;Thing" look like syntax rather than code + comment - add a space.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed and updated to use ArrayList

(baz [this]
(str "baz method for a Date: "
(.toString this)))
Foo;;Clojure Record
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as prior

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

(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). +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think Interface or Type need to be capitalized.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code font for code stuff


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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is some disagreement in pronouns here between "things" and "it". Protocol and Interface don't need to be capitalized here.


[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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Types doesn't need to be capitalized. I'm not sure what's actually meant by "Java types" here though.

* If you must extend a Java Class, use proxy
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Class doesn't need to be capitalized here and I would move proxy to the end

* If you want a on-off implementation of a Protocol/Interface, use reify
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"on-off" has a typo, but I think "anonymous instance" is better

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed