Pattern matching improvements

Georgi Stoyanov gstoyanov at live.com
Wed May 29 11:58:03 UTC 2024


Hey folks,
I'm really excited about the new Java features that keep getting added, and I had an idea I wanted to share. Have you thought about introducing a pattern matching feature similar to Scala’s, which allows for more concise and readable code?
In Scala, you can match a list that starts with a specific element without having to spell everything out. For example:
case List(_, 2, _*) => "I only care that this list has 2 as the second element"
case Person("Alice", age) => "It's Alice"

This approach makes the code less verbose and lets you focus directly on the elements you care about. It’s a lot cleaner than extracting elements and comparing them separately, which is what we do now in Java.
I think adding something like this to Java would be a big win, making pattern matching more powerful and the code more readable.
Just wanted to throw this idea out there and see what you all think.

Kind Regards,
Georgi Stoyanov

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20240529/bc2b3ca3/attachment.htm>


More information about the amber-dev mailing list