by geokon · 1y ago · 0 comments
The magic of protocol/records is that they work across library boundaries. A library may provide a record - and then you can extend the record with new protocols. Key is that it's all without needing to explicitly creating new agglomeration types.
You can take some Dog record from some pet-simulation library and then `extend-type` it with the IPageObject protocol and make the Dog record now something that can be displayed on a webpage
I don't understand. Did you not read my comment?
I just explained how your seemlessly extend records across library boundaries. It's not just solving with interfaces
And as a bonus observation, any time your example of how to use a programming feature involves animals it is a fake use case. It is a bad sign because it suggests that the benefit doesn't have a real world use.
I'll be honest I haven't touched Java in a long time, and I'm haven't had to extend my Clojure code this much before (but it's nice to know that I can - and use protocols with confidence)
Say LibraryA has some record..
LibraryB and LibraryC `extend-type` the record and implement additional functionality by implementing protocols..
Now you want to us both libraries in your application and use this record with both extensions. In Java this is forbidden due to diamond dependencies restrictions.
You can use multiple protocols in Java, but you can't have the protocols implemented (there are default methods but they don't have access to the Record's state)
Is there some way around this..? As far as I understand there isn't.
Furthermore, all the extensions need wrappers and need names. Nobody is doing this b/c you'd go nuts. But in Clojure it'd be a natural way to extend a library.
For instance
(defprotocol MyProtocol
(foo [x]))
(extend-type clojure.lang.PersistentVector
MyProtocol
(foo [x] (str "Vector of size " (count x))))
(extend-type clojure.lang.PersistentList
MyProtocol
(foo [x] (str "List of size " (count x))))
I can keep using vectors and lists as before and have new functionality. I don't need a new wrapper to think aboutI mean, this is false for Java. Creating a wrapper can end up being a big mess if you need to expose the functionality of the inner type. And if you need to preserve the actual underlying type, you end up needing wrap/unwrap all over the place. I think you're underestimating the cost of needing wrappers.
I would also argue that "it's rare enough in practice" because people just avoid doing it, either just avoiding using a library because it would be too much work to wrap, or more likely, just writing directly to the implementation instead of using an interface.
It most certainly does. The point is to help people lay eyes on the shape of code, in context of a public concept.
The post I've submitted is the "Clojure-magic" deep-dive you want!
Hopefully that helps.. Sorry if it's not exactly helpful
The protocol example at the end shows how it's done
https://eli.thegreenplace.net/2016/the-expression-problem-an...
If you write something like
(extend-type BinaryPlus
Evaluatable
(evaluate [this] (+ (evaluate (:lhs this)) (evaluate (:rhs this)))))
Then you are adding (and defining) the Evaluatable interface to a record BinaryPlus (which can be coming from a different library and already be implementing other interfaces)BinaryPlus records can still be used where they were used previously, but you can also use them with the enhancement you added
> I get the impression you'd need to explicitly create a new type class?
In the same way you need to use defprotocol in Clojure. You can implement existing type classes on types without defining a new type class.