Switch labels (null again), some tweaking
Brian Goetz
brian.goetz at oracle.com
Wed Apr 28 17:34:43 UTC 2021
> The benefit is twofold: not only does the user not have to write the
> stupid cases (imagine if Box had ten slots, would we want to write the
> 2^10 partial null cases?), but because we throw on the remainder, DA
> can treat the switch as covering all boxes, and be assured there are
> no leaks.
More on this last point. We don't yet have pattern assignment, but
totality and remainder are key to it. Suppose we have
Box<String> bs = ...
and we want to destructure:
Box(String s) = bs;
// s had better be DA here
The pattern on the LHS must be total, but can have remainder; the
statement is allowed to throw on the remainder, because the remainder
is, by definition, the "silly" matches. Here, the only remainder is
null; you can't destructure a null, so if you asked to destructure a
Box, you probably were assuming that it was a real box. So this
assignment NPEs on null (not unlike the corresponding switch).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20210428/8688a47d/attachment.htm>
More information about the amber-spec-experts
mailing list