Pattern matching on multiple objects

Brian Goetz brian.goetz at oracle.com
Sun Apr 23 18:00:18 UTC 2023


Yes, many languages that have pattern matching allow constant literals 
to be used as patterns ("constant patterns"), so

     case Point(0, _):

would match any point on the X axis.  The example Swaranga gave used 
constant patterns in this way, but that is orthogonal to the question 
being posed, which was about switching over multiple values at once.

Constant patterns have been discussed in early pattern matching 
writeups; we have left them on the "maybe for later" list, for a few 
reasons (and I'm glad we did.)  They may come into the lineup at some 
point, but in the meantime, there are higher-leverage features ahead of it.



On 4/23/2023 1:18 PM, David Alayachew wrote:
> Hello,
>
> For whatever reason, I remember seeing this as another potential 
> future direction for Java.
>
> $ build/windows-x86_64-server-release/images/jdk/bin/jshell 
> --enable-preview
> |  Welcome to JShell -- Version 21-internal
> |  For an introduction type: /help intro
>
> jshell> sealed interface ABC {
>    ...>     enum A implements ABC {Z}
>    ...>     record B(A a) implements ABC {}
>    ...>     record C(A a, B b) implements ABC {}
>    ...> }
> |  created interface ABC
>
> jshell> final ToIntFunction<ABC> d =
>    ...> abc ->
>    ...> switch (abc) {
>    ...>     case C(A.Z, B(A.Z)) -> 1;
>    ...>     default -> 0;
>    ...> };
>
> I think it was called constant patterns?
>
> Thank you for your time!
> David Alayachew
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20230423/7bbd3431/attachment.htm>


More information about the amber-dev mailing list