Fork me on GitHub
#clojure-dev
<
2018-11-29
>
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?

jumar07:11:26

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

andy.fingerhut13:11:43

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.

didibus17:11:32

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

didibus17:11:24

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?

didibus17:11:51

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.

didibus17:11:27

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

didibus17:11:11

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

didibus17:11:31

Say X was Dog and Y was LittleGirl

didibus17:11:28

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.

didibus17:11:00

And this is why tying inheritance to polymorphism is bad.

didibus17:11:15

This is partially solved in Java with interfaces.

didibus17:11:44

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