case null vs case dominance
Remi Forax
forax at univ-mlv.fr
Mon Jun 7 09:51:29 UTC 2021
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.
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;
};
Also with default, the spec says that the code below should compile (and it works with javac),
because default and case null are disjoint in term of type, but it feels wrong to me.
Object o = null;
var value = switch(o) {
default -> 0;
case null -> 0;
};
System.out.println(value);
The problem is that sometimes 'default' acts as a total pattern sometimes it does not, if there is a case null. I wonder if it's not better to ask users to put the case null on top.
Rémi
More information about the amber-spec-experts
mailing list