case null vs case dominance
Brian Goetz
brian.goetz at oracle.com
Mon Jun 7 15:06:20 UTC 2021
On 6/7/2021 5:51 AM, Remi Forax wrote:
> Hi all,
> the first part of the message is about javac error message that could be improved,
> the second part is about the current spec being not very logical.
>
> With this code
>
> Object o = null;
> var value = switch(o) {
> //case null -> 0;
> case Object __ -> 0;
> case null -> 0;
> };
> System.out.println(value);
>
> The error message is
> PatternMatching101.java:70: error: this case label is dominated by a preceding case label
> case null -> 0;
> ^
>
> The error message is wrong here, because it's 'case null' and you can put a case null where you want but below a total pattern, so the error mesage should reflect that.
But the case null *is* dominated by the total pattern, and therefore dead.
> Here is an example that compiles showing that case null can be below a case String, which it dominates
>
> Object o = null;
> var value = switch(o) {
> case String s -> 0;
> case null -> 0;
> default -> 0;
> };
In this case, the String pattern is not total, so it does not dominate null.
More information about the amber-spec-experts
mailing list