<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">
We take inspiration from many sources, including many existing languages that have pattern matching.
<div class=""><br class="">
</div>
<div class="">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.). </div>
<div class="">
<div><br class="">
<blockquote type="cite" class="">
<div class="">On May 29, 2024, at 7:58 AM, Georgi Stoyanov <<a href="mailto:gstoyanov@live.com" class="">gstoyanov@live.com</a>> wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div class="elementToProof" style="font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; font-family: Calibri, Helvetica, sans-serif; font-size: 11pt;">
Hey folks,</div>
<div class="elementToProof" style="font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; text-align: left; margin-top: 1em; margin-bottom: 1em; font-family: Calibri, Helvetica, sans-serif; font-size: 11pt;">
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?</div>
<div style="font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; text-align: left; margin-top: 1em; margin-bottom: 1em; font-family: Calibri, Helvetica, sans-serif; font-size: 11pt;" class="">
In Scala, you can match a list that starts with a specific element without having to spell everything out. For example:</div>
<div class="elementToProof" style="font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; font-family: Calibri, Helvetica, sans-serif; font-size: 11pt;">
case List(_, 2, _*) => "I only care that this list has 2 as the second element"<br class="">
case Person("Alice", age) => "It's Alice"<br class="">
<br class="">
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.</div>
<div style="font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; text-align: left; margin-top: 1em; margin-bottom: 1em; font-family: Calibri, Helvetica, sans-serif; font-size: 11pt;" class="">
I think adding something like this to Java would be a big win, making pattern matching more powerful and the code more readable.</div>
<div style="font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; text-align: left; margin-top: 1em; margin-bottom: 1em; font-family: Calibri, Helvetica, sans-serif; font-size: 11pt;" class="">
Just wanted to throw this idea out there and see what you all think.</div>
<div class="elementToProof" style="font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; text-align: left; margin-top: 1em; margin-bottom: 1em; font-family: Calibri, Helvetica, sans-serif; font-size: 11pt;">
<br class="">
Kind Regards,<span class="Apple-converted-space"> </span><br class="">
Georgi Stoyanov</div>
</div>
</blockquote>
</div>
<br class="">
</div>
</body>
</html>