Fork me on GitHub
Yehonathan Sharvit07:11:30

Back to the topic of Records in Clojure and how they differ from Objects in OOP, in the datatype official material, there is this claim: “Tying polymorphism to inheritance is bad” What does it mean exactly?


a bit off topic, but I think this is more suitable for #clojure

Yehonathan Sharvit08:11:11

See above, there was a discussion about records vs. objects

Yehonathan Sharvit08:11:29

This question asks for clarification


I believe the general idea is that Java style implementation inheritance, where subclasses by default inherit the implementation of their superclass, introduces too much coupling between their implementations.


It's talking about the difference between derived classes, using the extends keyword in Java, and implementing an interface, the implements keyword in Java


So it's saying, what if I want the "talk" function to work with more than one thing. I don't want to write a (talk-x x) (talk-y y), I'd like to instead have (talk x) (talk y) where talk is polymorphic on its first argument's type. How can I do that?


In classic Java/C++ OOP, you can only do it by making the type of y Y, inherit from the type of x X or vice versa.


So simply to make talk polymorphic, you're now obligated to create an inheritance hierarchy.


Even though X might not be a Y, it might only share the fact that it also can talk. Nothing more.


Say X was Dog and Y was LittleGirl


Now you might need to create a Mammal parent class with a talk function. But now you also have Robots that can talk? So your hierarchy is screwed.


And this is why tying inheritance to polymorphism is bad.


This is partially solved in Java with interfaces.


And Clojure Protocols are mostly an open variant of Java interfaces.