New candidate JEP: 455: Primitive types in Patterns, instanceof, and switch (Preview)
Attila Kelemen
attila.kelemen85 at gmail.com
Tue Sep 26 10:10:56 UTC 2023
I assume this won't be an issue with the nullability JEP implemented (not
sure where that is in the roadmap though)? That is, in this case we could
expect things like this to work, right?
```
switch (p) {
case Pair!(Pair?(A a, B b), Pair?(C c, D d)): ...
}
```
or
```
switch (p) {
case Pair!(Pair!(A a, B b), Pair!(C c, D d)): ...
}
```
And if that is the case, then trying to be tricky now would be a waste
anyway.
Brian Goetz <brian.goetz at oracle.com> ezt írta (időpont: 2023. szept. 26.,
K, 3:19):
> The gap between exhaustiveness and totality (which we call "remainder") is
> indeed somewhat counterintuitive at first, because we would like for these
> two words to mean the same thing. There were many, many discussions
> regarding this in Record Patterns. This JEP extends the surprise a little
> bit, but only in a quantitative, not qualitative way; unboxing conversions
> are another place where we encounter a gap between these two similar
> concepts.
>
> The first part of the example (Box<Box<String>>) is not new to this JEP;
> this is how nested patterns work. (If you think about it for long enough,
> you realize that the alternatives are all nearly intolerable.) The
> Box<Integer> example just adds one more form of remainder.
>
> Imagine it worked the way your intuition tells you it should work. I'll
> start with the old case, nested record patterns. Now, suppose you have:
>
> record Pair<T,U>(T t, U u) { }
>
> ...
>
> Pair<Pair<A, B>, Pair<C, D>> p = ...
>
> switch (p) {
> case Pair(Pair(A a, B b), Pair(C c, D d)): ...
> }
>
> Under the David rules, this switch is not exhaustive, because it matches
> neither Pair(null, _) or Pair(_, null). So what cases would you have to
> add to the switch to satisfy the compiler's complaints of
> non-exhaustiveness? Write the code, and then tell me if you want to
> program in that language...
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/jdk-dev/attachments/20230926/5aeb2a85/attachment-0001.htm>
More information about the jdk-dev
mailing list