News for hackers. Stuff that matters.
Re:Java 21: What’s New? by heavyset_go on Wednesday August 09, 2023 @06:19PM (#37069163)
You can have a parent class/interface with many subclasses/implementations.

You can then use pattern matching to switch on instances of those subclasses while destructuring objects to access attributes at the same time. Kind of like enums and pattern matching in Rust.

In your example there is no reason to use `instanceof` with `p`, you already know what it is.

Re:Java 21: What’s New? by The_Colonel on Thursday August 10, 2023 @01:50AM (#37072429)
> 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();
Re:Java 21: What’s New? by kaba0 on Thursday August 10, 2023 @04:07AM (#37073364)
There 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.
Re:Java 21: What’s New? by heavyset_go on Thursday August 10, 2023 @04:52PM (#37081631)
> 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.