News for hackers. Stuff that matters.
Re:Data-oriented design or why you might shoot yourself in t... by SPBS on Monday June 28, 2021 @01:05PM (#27664453)
> Inheritance is much less troublesome in, say, Smalltalk, since the language is dynamically typed. If someone expects you to implement Foo, you can (almost always) just implement its relevant methods without explicitly extending the class.

Sorry, I don't understand this sentence. Isn't inheritance simply a way to avoid writing duplicate code? If you write the code to implement methods, isn't that not inheritance anymore?

Re:Data-oriented design or why you might shoot yourself in t... by throwaway894345 on Monday June 28, 2021 @01:18PM (#27664609)
Inheritance conflates code reuse ("avoid writing duplicate code") with polymorphism (allowing for multiple different instances to implement the same interface). It also allows for trampolining method calls up and down a hierarchy (a method in a base class might call another method which might be overridden by another class in the hierarchy).

Outside of OOP, we use composition for reuse and interfaces for polymorphism, and we don't trampoline method calls up and down a hierarchy because it's (probably?) always a bad idea. When we really need reuse and polymorphism, we can use both composition and interfaces, since the two are correctly orthogonal.

Re:Data-oriented design or why you might shoot yourself in t... by spacechild1 on Tuesday June 29, 2021 @03:34PM (#27680706)
> Inheritance conflates code reuse ("avoid writing duplicate code") with polymorphism (allowing for multiple different instances to implement the same interface).

Note that languages like C++ allow for inheritance without polymorphism, i.e. pure implementation inheritance.

However, I also think that composition should be preferred whenever possible.

Re:Data-oriented design or why you might shoot yourself in t... by ratww on Monday June 28, 2021 @01:46PM (#27665006)
What the grandparent post means is that in dynamic languages you can just implement one of the "base" methods yourself instead of inheriting from a class that's bigger than you need, in order to avoid problems. I personally don't have an opinion on that, but it's not something I'd do myself.

Also, like the sibling said, inheritance is a tool that does multiple things: code reuse, which we call implementation inheritance, being the one everyone hates (the age-old advice is to use composition for code reuse instead), and interface inheritance being the one everyone loves.