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 5 commits into
base: master
from

Conversation

@iku000888
Copy link
Contributor

@iku000888 iku000888 commented Jul 21, 2017

While a lot of people are having fun at EuroClojure, I attempted to write a guide that I think would have helped me a lot when I was starting to learn Clojure :)

I would appreciate any feedback.

Thanks!

Problem statement

  1. datatype constructs provided by Clojure have overlapping capabilities and could be confusing to beginners (like my self 1 year ago), albeit provided for different purposes.
  2. Official references explain the rationale and capabilities of these constructs really well but are described in separate sections. In order to get a picture of how these constructs fit together, one must read through several sections of the reference.
  1. More examples for these constructs wanted

Related issue #152

Goal

After reading this guide, one should understand what each of the constructs are good for as well as how to use them.

Scope

  • proxy(with some necessary java interop to elaborate the point)
  • reify
  • defrecord
  • protocols
  • along with minimal examples that can be tested at the repl
Copy link
Member

@puredanger puredanger left a comment

It's a good start but I think it would help to clarify what the goal is.

@@ -0,0 +1,153 @@
= Understanding Clojure's Polymorphism

This comment has been minimized.

@puredanger

puredanger Aug 4, 2017
Member

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.

This comment has been minimized.

@iku000888

iku000888 Aug 11, 2017
Author Contributor

multimethods are out of scope of this guide, so not about polymorphism.

This comment has been minimized.

@iku000888

iku000888 Aug 11, 2017
Author Contributor

Updated title.

I do mention deftype, but not in depth believing that by default, one should reach for records.
Feedback would be appreciated though.


== Goals of this guide

Clojue supports several constructs for speaking to the Java world

This comment has been minimized.

@puredanger

puredanger Aug 4, 2017
Member

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

This comment has been minimized.

@iku000888

iku000888 Aug 11, 2017
Author Contributor

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.

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.

This comment has been minimized.

@puredanger

puredanger Aug 4, 2017
Member

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

This comment has been minimized.

@iku000888

iku000888 Aug 11, 2017
Author Contributor

Good point :)

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

This comment has been minimized.

@puredanger

puredanger Aug 4, 2017
Member

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

This comment has been minimized.

@iku000888

iku000888 Aug 11, 2017
Author Contributor

Dropped

== 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:

This comment has been minimized.

@puredanger

puredanger Aug 4, 2017
Member

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.

This comment has been minimized.

@iku000888

iku000888 Aug 11, 2017
Author Contributor

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.

== 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

This comment has been minimized.

@puredanger

puredanger Aug 4, 2017
Member

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

This comment has been minimized.

@iku000888

iku000888 Aug 11, 2017
Author Contributor

fixed

[source,clojure-repl]
----
user=> (extend-protocol IBaz
Date;;Thing from Java

This comment has been minimized.

@puredanger

puredanger Aug 4, 2017
Member

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

This comment has been minimized.

@iku000888

iku000888 Aug 11, 2017
Author Contributor

fixed and updated to use ArrayList

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

This comment has been minimized.

@puredanger

puredanger Aug 4, 2017
Member

same as prior

This comment has been minimized.

@iku000888

iku000888 Aug 11, 2017
Author Contributor

fixed

"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). +

This comment has been minimized.

@puredanger

puredanger Aug 4, 2017
Member

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

This comment has been minimized.

@iku000888

iku000888 Aug 11, 2017
Author Contributor

fixed


* 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

This comment has been minimized.

@puredanger

puredanger Aug 4, 2017
Member

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

This comment has been minimized.

@iku000888

iku000888 Aug 11, 2017
Author Contributor

fixed

@iku000888
Copy link
Contributor Author

@iku000888 iku000888 commented Aug 5, 2017

@iku000888 iku000888 force-pushed the iku000888:clj-datatype-guide branch 2 times, most recently from abd428a to 4462095 Aug 11, 2017
@iku000888 iku000888 force-pushed the iku000888:clj-datatype-guide branch from 4462095 to d5f86fe Aug 11, 2017
@iku000888
Copy link
Contributor Author

@iku000888 iku000888 commented Aug 11, 2017

@puredanger

Overall thanks for making this so much better!
I think I addressed your comments, but let me know if I missed something or there is anything else to do :)

@iku000888
Copy link
Contributor Author

@iku000888 iku000888 commented Sep 11, 2017

@puredanger ping?

@iku000888
Copy link
Contributor Author

@iku000888 iku000888 commented Nov 30, 2017

@puredanger Is there anything I can do to carry this forward?
I understand Clojure 1.9 is just around the corner, so no rush :)

@puredanger
Copy link
Member

@puredanger puredanger commented Nov 30, 2017

Post 1.9 I'm going to try to work my way through some of the backlog here, I'll let you know.

@iku000888
Copy link
Contributor Author

@iku000888 iku000888 commented Mar 7, 2018

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

This comment has been minimized.

@puredanger

puredanger Apr 6, 2018
Member

Proxies should probably be the least frequently used so I don't like starting this guide with it. Should move to the end.


== Proxy a Java class and/or Interfaces

The proxy macro can be used to create an adhoc object that extends a Java Class.

This comment has been minimized.

@puredanger

puredanger Apr 6, 2018
Member

proxy should have code syntax highlighting. Class should not be capitalized here.

== 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

This comment has been minimized.

@puredanger

puredanger Apr 6, 2018
Member

remove "the good old", code format for java.util.ArrayList

(.size px)
;; => 10
----
The ad hoc object can also implement Java Interfaces:

This comment has been minimized.

@puredanger

puredanger Apr 6, 2018
Member

Interfaces should not be capitalized here.


== Leaving Java with defrecord

So far this is all dealing with Java stuff from Clojure. +

This comment has been minimized.

@puredanger

puredanger Apr 6, 2018
Member

"Java stuff" is too informal

nil
----

== Leaving Java with defrecord

This comment has been minimized.

@puredanger

puredanger Apr 6, 2018
Member

Why is this called "Leaving Java"?

nil
----

Records are nicer than Java classes for the reasons described in the https://clojure.org/reference/datatypes#_deftype_and_defrecord[reference].

This comment has been minimized.

@puredanger

puredanger Apr 6, 2018
Member

I think this could use some elaboration.

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.

This comment has been minimized.

@puredanger

puredanger Apr 6, 2018
Member

... or don't have map semantics

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

This comment has been minimized.

@puredanger

puredanger Apr 6, 2018
Member

Don't need to editorialize in the header

* 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:

This comment has been minimized.

@puredanger

puredanger Apr 6, 2018
Member

code font for ArrayList and Foo

"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). +

This comment has been minimized.

@puredanger

puredanger Apr 6, 2018
Member

code font for java.util.Date


[source,clojure-repl]
----
user=> (defprotocol IBaz

This comment has been minimized.

@puredanger

puredanger Apr 6, 2018
Member

I think it would be better if this was a real example rather than IBaz / Foo.


[source,clojure-repl]
----
user=> (defrecord Foo [a b]

This comment has been minimized.

@puredanger

puredanger Apr 6, 2018
Member

Can we make this a real example?

----
(import 'java.util.ArrayList)
(def px (let [atm (atom [])]

This comment has been minimized.

@puredanger

puredanger Apr 6, 2018
Member

Can we get a more meaningful example?

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

This comment has been minimized.

@puredanger

puredanger Apr 6, 2018
Member

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:

This comment has been minimized.

@puredanger

puredanger Apr 6, 2018
Member

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

== Take away
To wrap up, here are some rules of thumb:

* Prefer protocols and records over Java Types; stay in Clojure

This comment has been minimized.

@puredanger

puredanger Apr 6, 2018
Member

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

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

This comment has been minimized.

@puredanger

puredanger Apr 6, 2018
Member

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


* 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

This comment has been minimized.

@puredanger

puredanger Apr 6, 2018
Member

Protocol / Interface don't need to be capitalized

Copy link
Member

@puredanger puredanger left a comment

This is a good start but it real needs more motivating examples and prose. This has a lot of "how" but not enough "why". What about gen-class?

@iku000888
Copy link
Contributor Author

@iku000888 iku000888 commented Apr 8, 2018

Thanks for the feedback! Will digest them slowly... 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants
You can’t perform that action at this time.