Feedback on nulls in switch
John Rose
john.r.rose at oracle.com
Tue Aug 11 00:42:50 UTC 2020
On Aug 10, 2020, at 4:03 PM, Stephen Colebourne <scolebourne at joda.org> wrote:
>
> Given:
> box instanceof Object o
> o is non-null. And given:
> switch (box) {
> case Box b:
> }
> b is non-null.
That latter is not necessarily true and I think you need a better example.
The b can be null because the case is the last case and therefore (not otherwise) may be total. When I see the last case in a switch I expect it to be a catch-all, don’t you? Here it depends on the type of box up in the switch header. I suppose it’s a Box. So that last case catches whatever the previous zero cases did t catch.
If Box b = box; is valid, and it sure looks that way, then that last case is indeed a catch all. And in that case, unless we enact a specific anti I’ll policy, any and all nulls will flow through that point.
I think we all are going to have to learn to read the bottom of a switch as a probable catch-all.
Maybe we want to add a modifier to enforce the assumption, and exclude the implicit default; I’ve suggested ‘final case’ as a cite bikeshed color. There are many others.
None of that eases your discomfort with null flows. But it does undercut your claims of illegibility.
More information about the amber-dev
mailing list