Re:Inheritance was invented as a performance hack
by ptato on Friday April 30, 2021 @07:36AM
(#26992133)
Would that copy-paste code ever amount to more than some getter methods, like the get_speed() example you provided? If the speed field had some special behaviour, couldn't you wrap it in its own Speed type, with its own methods, and use that from the enclosing types?
[ Parent
| on HN ]
Re:Inheritance was invented as a performance hack
by adrianmsmith on Friday April 30, 2021 @08:01AM
(#26992306)
I think the solution you've proposed is probably how you'd do it - but isn't inheritance more elegant in this case (i.e. using a language which supports inheritance if this is important to you)
Re:Inheritance was invented as a performance hack
by sanderjd on Friday April 30, 2021 @10:08AM
(#26993657)
No, inheritance is not an elegant way to mix in shared data and behavior. It can seem like it is in a simple case where there is only one set of data and behavior you want to mix in, but it scales very poorly. Nothing is fundamentally a single kind of thing, and multiple inheritance is a mess. It is more elegant to mix the behavior in through delegation, because it scales to however many things you want to mix in. Carrying on the synthetic example in this thread, you can mix in Speed and Pedals into Bicycle but Speed and Cylinders into Car.