Pattern matching improvements

Georgi Stoyanov gstoyanov at live.com
Wed May 29 14:49:23 UTC 2024


Hi, thank you for the quick response!

Yes, regarding the lists, it's indeed weak. But regarding actually the objects and matching them directly based on their values, without need to write something like case Person(name, age) where name == "Alice" && age == 40, but just case Person("Alice", 40) is really good feature, isn't it?


Kind Regards,
Georgi Stoyanov

________________________________
From: Brian Goetz <brian.goetz at oracle.com>
Sent: Wednesday, May 29, 2024 4:21 PM
To: Olexandr Rotan <rotanolexandr842 at gmail.com>
Cc: Georgi Stoyanov <gstoyanov at live.com>; amber-dev at openjdk.org <amber-dev at openjdk.org>
Subject: Re: Pattern matching improvements

Yes, these sorts of arguments are why I describe it as a weak candidate.  One only needs to think about it for a minute or so to come up with a whole pile of “follow on features” (“can I have a wildcard indicator that means any number of intervening elements”, “can I have a way to express an element matching P comes before an element matching Q”, etc).

List patterns are fundamental in languages where cons-lists (such as Lisp or Haskell) are a foundational data structure in the langauge.  In that case, matching head/tail becomes a fundamental feature.  But that’s not Java.

On May 29, 2024, at 9:12 AM, Olexandr Rotan <rotanolexandr842 at gmail.com<mailto:rotanolexandr842 at gmail.com>> wrote:

I would like to say that, imo, point that list (or any sequential collection/array) patterns make code readable is a really brave assumption to say the least. I had some experience reading code like this in C#, and to say that I was not impressed would be an understatement. For me, it gives off strong regex vibes, which are, meanwhile really powerful tool, known to be barely readable.
Also example you have provided above seems kind of python-like in a way that in which situation would you expect List OR single object as input. Unless there are union types (which I'm also not a fan of to be fair), your example is not from the java world, in my opinion.
The more realistic applixation for such patterns is match "shape" of collection, if you already know you are dealing with collection, but I'm not sure that game worth the candle to be fair. I guess such patterns could be more effective then traditional ways to do things that they are intended to substitute, so maybe in some caseslike data-oriented programming performance improvements could be significant, but besides I would think ten times before introducing another regex into the language.

On Wed, May 29, 2024, 16:02 Brian Goetz <brian.goetz at oracle.com<mailto:brian.goetz at oracle.com>> wrote:
We take inspiration from many sources, including many existing languages that have pattern matching.

I think what you’re asking is: “have you considered List patterns”.  And indeed, we have.  List patterns are the dual to list literals (more generally, collection patterns are the dual to collection literals); we would consider introducing collection patterns when we are able to treat them holistically with collection literals.  However, of all the pattern matching features one could craft “have you considered X” for, list patterns are among the weakest, and so these are likely to be lower priority than other pattern matching features in the pipeline (e.g., varargs patterns, constant patterns, etc.).

On May 29, 2024, at 7:58 AM, Georgi Stoyanov <gstoyanov at live.com<mailto:gstoyanov at live.com>> wrote:

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/40115764/attachment.htm>


More information about the amber-dev mailing list