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

Conversation

iku000888
Copy link
Contributor

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

Choose a reason for hiding this comment

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

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


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

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.
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 :)

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

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

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

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


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

@iku000888
Copy link
Contributor Author

iku000888 commented Aug 5, 2017 via email

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

@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

@puredanger ping?

@iku000888
Copy link
Contributor Author

@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

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

@puredanger ?

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


== Proxy a Java class and/or Interfaces

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.

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

Choose a reason for hiding this comment

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

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

(.size px)
;; => 10
----
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.


== Leaving Java with defrecord

So far 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.

"Java stuff" is too informal

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"?

nil
----

Records are nicer than Java classes 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.

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

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

* 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:
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 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). +
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 java.util.Date


[source,clojure-repl]
----
user=> (defprotocol IBaz
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 would be better if this was a real example rather than IBaz / Foo.


[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?

----
(import 'java.util.ArrayList)

(def px (let [atm (atom [])]
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 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
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.

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

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


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

Choose a reason for hiding this comment

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

Protocol / Interface don't need to be capitalized

Copy link
Member

@puredanger puredanger left a comment

Choose a reason for hiding this comment

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

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

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

@wildwestrom
Copy link

Hey @iku000888, can I pick up where you left off?
I'd love to see this added to the site!

@iku000888
Copy link
Contributor Author

@wildwestrom Of course! Thanks for asking. Happy to let go of any credit to me as well.

@wildwestrom
Copy link

@wildwestrom Of course! Thanks for asking. Happy to let go of any credit to me as well.

Ok, I'm thinking I'll fork your repo and then fetch the upstream changes. Is there a cleaner way to do it though? I'd rather not make a new PR and just edit this one directly.

@iku000888
Copy link
Contributor Author

@wildwestrom
Tried adding you as a collaborator on my fork. Perhaps you can push on this branch?

@wildwestrom
Copy link

@iku000888 I'm having a bit of trouble with that.

$ git push
ERROR: Permission to iku000888/clojure-site.git denied to wildwestrom.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

@iku000888
Copy link
Contributor Author

@wildwestrom On my end it says the invite is still pending. Is there anything in your mail?

@wildwestrom
Copy link

@iku000888 That was it, my email client was off.

@iku000888
Copy link
Contributor Author

@wildwestrom
Awesome! Wishing you a productive time ahead!

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

Successfully merging this pull request may close these issues.

4 participants