3 ms·
> You can have a parent class/interface with many subclasses/implementations. Sure, but in Java the standard handling of that is polymorphism. instanceof has b
by The_Colonel 3y ago
> You can have a parent class/interface with many subclasses/implementations.
Sure, but in Java the standard handling of that is polymorphism. instanceof has been so far a bit of an antipattern, breaking out of the OOP model.
> In your example there is no reason to use `instanceof` with `p`, you already know what it is.
That's my point. This seems like a very limited version of destructuring if you can use it only when you don't know the type. Like in JavaScript you'd routinely write something like:
const {x, y} = getPoint();
- kaba0 3y agoThere is subtyping. A more practical (though not necessarily realistic) example might be: sealed interface List<T> permits Cons<T>, None<T> { record Cons<T>(T head, List<T> tail) {} record None<T>() {} } // some function only wanting the first elem of a list switch (List<T> list) { case Cons<>(T head, var tail) -> // do something default -> {} } If you have used Haskell or similarly strongly typed FP language, you can see how great pattern matching can be.
- heavyset_go 3y ago> Sure, but in Java the standard handling of that is polymorphism. instanceof has been so far a bit of an antipattern, breaking out of the OOP model. I think that's deliberate, Java is moving towards the modern programming zeitgeist that doesn't really care about strict adherence to the OOP model. This is a feature that comes from languages that don't adhere to Java's OOP principles. I didn't downvote you, by the way. Don't know why that happened.