News for hackers. Stuff that matters.
Re:If Inheritance is so bad, why does everyone use it? by mrkeen on Thursday April 11, 2024 @05:11AM (#40000071)
> For me the main point is (runtime) polymorphism. E.g. you have a function that takes a general type, and you can pass multiple specific types and it will do the right thing.

The runtime part is what I dislike. If I have a fruit which is an apple or a banana, I can't pass that to a method expecting an apple or banana. It can only be passed as a fruit.

> And if you want to avoid huge if-else statements, you should put the code for the special cases in the classes, not in each function that operates on them.

This is common OO wisdom that I strongly disagree with. For example, in my program I have a few types (Application, Abstraction, Variable, etc.), and a lot of transformations to perform on those types (TypeCheck, AnfConvert, ClosureConvert, LambdaLift, etc.).

I prefer to have all the type-checking code inside the TypeCheck module, and all the closure-converting code inside the ClosureConvert module. I'd take the "huge if-else" statements inside TypeCheck rather than scatter typechecking across all my datatypes.

Re:If Inheritance is so bad, why does everyone use it? by rtz121 on Thursday April 11, 2024 @12:43PM (#40004161)
> If I have a fruit which is an apple or a banana, I can't pass that to a method expecting an apple or banana. It can only be passed as a fruit.

You can by overriding the method on apple or banana. If your method is on some other object, then yes, you cannot do this unless your programming language supports multiple dispatch.

Re:If Inheritance is so bad, why does everyone use it? by zbentley on Thursday April 11, 2024 @02:31PM (#40005288)
> I prefer to have all the type-checking code inside the TypeCheck module

There are ways to have your cake and eat it too, here, at least in some languages and patterns.

For example, in Go you could define "CheckType" as part of the interface contract, but group all implementors' versions of that method in the same file, calling out to nearby private helper functions for common logic.

Ruby's open classes and Rust's multiple "impl" blocks can also achieve similar behavior.

And yeah, sure, some folks will respond with "but go isn't OO", but that's silly. Modelling polymorphic behavior across different data-bearing objects which can be addressed as implementations of the same abstract type quacks, very loudly, like a duck in this case.

Re:If Inheritance is so bad, why does everyone use it? by Capricorn2481 on Friday April 12, 2024 @08:48PM (#40019284)
> And yeah, sure, some folks will respond with "but go isn't OO", but that's silly. Modelling polymorphic behavior across different data-bearing objects which can be addressed as implementations of the same abstract type quacks, very loudly, like a duck in this case.

It's less "this is not OO" and more this is not inheritance, which is why a lot of people are saying you can find more elegant solutions (like this) rather than use inheritance for no clear benefit.

Re:If Inheritance is so bad, why does everyone use it? by kaba0 on Thursday April 11, 2024 @04:42PM (#40006604)
> The runtime part is what I dislike. If I have a fruit which is an apple or a banana, I can't pass that to a method expecting an apple or banana. It can only be passed as a fruit.

Heh? An apple is a fruit, you can pass it to any place expecting the former. Like, this is Liskov’s substitution’s one half.

With generics, you can be even more specific (co/in/contra-variance).

Re:If Inheritance is so bad, why does everyone use it? by cornstalks on Thursday April 11, 2024 @05:13PM (#40006866)
I think GP is talking about something like this:

    Fruit* fruit = new Apple();
    ConsumeApple(fruit);  // Doesn't work; requires Apple*

    fruit = new Banana();
    ConsumeBanana(fruit);  // Doesn't work; requires Banana*

    ConsumeFruit(fruit);  // Okay, function signature is void(Fruit*)
Re:If Inheritance is so bad, why does everyone use it? by sfn42 on Friday April 12, 2024 @04:13AM (#40010419)
But why would you want to be able to pass an apple to a function expecting a banana? Doesn't even make sense, the whole point is that the type system forces you to consider this stuff.

If OP wanted to be allowed to do whatever and just have the software fail at runtime, JS is right there.

Re:If Inheritance is so bad, why does everyone use it? by mrkeen on Friday April 12, 2024 @06:31AM (#40011084)
> But why would you want to be able to pass an apple to a function expecting a banana?

I would never. I know that I'm holding a banana, I want to pass it to a method that receives a banana. But what I can't do is put my banana through a rotateFruit function first, because then Java will forget that it's a banana and start treating it as a fruit.

Re:If Inheritance is so bad, why does everyone use it? by kaba0 on Friday April 12, 2024 @07:30AM (#40011380)
Java has generic functions since forever, something like `<T extends Fruit> T rotateFruit(T fruit)` would return your banana typed object just fine.
Re:If Inheritance is so bad, why does everyone use it? by skydhash on Friday April 12, 2024 @08:57AM (#40012071)
Inheritance is just a tool, that you use if it fits you software architecture. You use other tools, like generics, interface, where it makes senses to ise them. I think people wants the one true way to not have to design their software. And when they use inheritance in a ways that does not fit, they blame inheritance.
Re:If Inheritance is so bad, why does everyone use it? by mrkeen on Saturday April 13, 2024 @04:23AM (#40021459)
But then I lose polymorphic dispatch, which is where this thread started.
Re:If Inheritance is so bad, why does everyone use it? by kaba0 on Saturday April 13, 2024 @05:33AM (#40021765)
If you pass your object to another function, you only get static dispatch. What you want instead in this case, is a simple instance method. Then yourApple.rotateFruit() would do what you want, and rutateFruit could be an interface method declared in Apple, the superclass/interface.