by TurboHaskal · 10y ago · 0 comments
This is a curious comment, and I'd like to hear some elaboration.
I used C++ for years, then 2 years as a full-time Clojure dev, and I don't see any OO parallels in Clojure at all.
* clojure protocols map to interfaces (abstract classes for C++ folks)
* one can define records that satisfy protocol(s). This is one way to do OO in clojure, happens to map to the underlying JVM mechanisms for JVM optimizations to kick in.
* Gamma et al., Bloch and many others tell to prefer composition over inheritance. Clojure makes this guideline easy to follow.
* overall the OO leaders also recommend making classes "value classes" (everything immutable; smart copy semantics; etc) as often as humanly possible.
* Multimethods allow arbitrary dispatch (most versatile kind of polymorphism) and there is also a faster method when you only want to dispatch by protocols (the (sadly) 'usually' only way to dispatch in most OO languages) that reuses JVM notion of classes and interfaces behind the curtains.
* What you don't get is encapsulation, (and many, a great many authors cite encapsulation to be the definite trait of an OO language,) it can also be argued that immutable values do not need encapsulation per se. A value should be able to stand on its own, convey the semantics it needs & so on..
Yes this is what I was referring to, Clojure is not OO in the way that many think of OO.
But your other points are right on.
Now there forms many schools of opinion, and in many of them it suffices to have data immutable and value-like, and suddenly it doesn't matter if the information is hidden into opaque memory addresses or not.
Interfaces/traits maybe (z.i is quite crusty) but it's just as good at polymorphism.
Mutability though yes -- hope you're aware of https://pyrsistent.readthedocs.io/en/latest/ I use it in every project I write.
One thing that is not perfect is the way Python treats classes; the mechanism is very much like in Java or C#. Multiple dispatch requires some handiwork, although it's not half bad in a highly dynamic (so many things are first class in Python!) language. It kinda isn't Python's fault but there you go when trying to introduce concepts to fellow developers not in the most usual python guidebooks. And a part of the problem is Django, which is nice and all, but entails some highly unpythonic design choices and people around here take Django's way as gospel.
Pyrsistent is great. But I'd rather Python 4000 or something would go the way of scala or swift where you have immutable values by default and mutable values only when you specify it.