[External] : Re: Remainder in pattern matching
Brian Goetz
brian.goetz at oracle.com
Thu Apr 21 17:17:08 UTC 2022
In the first example, the first case will match all Boxes except null
and Box(null). The second case will match both of these values, so
there is no remainder.
In the second example, the first case will match. This is because the
nested pattern `String s` is unconditional on the binding of Box(Box( * )).
Note that you will want to include the generics in the patterns:
case Box<Box<String>>(Box<String>(String s)):
until we are able to infer these; raw record patterns are not helpful.
On 4/21/2022 2:25 AM, Ali Ebrahimi wrote:
> Hi Brian,
> Please clear me for this case?
>
> On Fri, Apr 1, 2022 at 6:26 PM Brian Goetz <brian.goetz at oracle.com> wrote:
>
>
> Box<Box<String>> bbs = new Box(null);
> switch (bbs) {
> case Box(Box(String s)): ...
> case null, Box b: ...
> }
>
> has no remainder and will not throw. Box(null) doesn't match the
> first
> pattern, because when we unroll to what amounts to
>
> if (x instanceof Box alpha && alpha != null && alpha.value()
> instanceof Box beta && beta != null) {
> s = beta.value(); ...
> }
> else if (x == null || x instanceof Box) { ... }
>
> What about this
> Box<Box<String>> bbs = new Box(new Box(null));
> switch (bbs) {
> case Box(Box(String s)): ...
> case null, Box b: ...
> }
>
> Which case matches switch input?
>
>
> --
>
> Best Regards,
> Ali Ebrahimi
More information about the amber-spec-observers
mailing list