New candidate JEP: 455: Primitive types in Patterns, instanceof, and switch (Preview)
Attila Kelemen
attila.kelemen85 at gmail.com
Tue Sep 26 15:09:03 UTC 2023
>
>
> As it turns out, your first example is nonsensical:
>
>
> ```
> switch (p) {
> case Pair!(Pair?(A a, B b), Pair?(C c, D d)): ...
> }
> ```
>
>
> Here, the nested pattern `Pair?(A a, B b)` makes no sense, because it is a
> record pattern, and a record pattern *cannot* match null (because it needs
> a non-null receiver on which to invoke the accessors and/or
> deconstructor.) So even in a world with emotional type patterns, this
> pattern would be rejected by the compiler as illegal.
>
Yeah, I don't know how I missed that, because I actually considered the
same thing for `case Pair?(Pair? a, Pair? b)` and realized that it makes no
sense ...
But then I assume if we had `Pair` declared as `record Pair<A, B>(A? a, B?
b) {}`, then I could use this to not match a `Pair` instance with null
fields:
```
switch (p) {
case Pair!(String! a, String! b): ...
}
```
And writing `String` would be inferred to be `String?` in this case.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/jdk-dev/attachments/20230926/11cfd420/attachment-0001.htm>
More information about the jdk-dev
mailing list