Another case where the spec is weird,
i've converted a project that generate a visitor from a grammar (something like
yacc) to use a switch on type instead.
Sometimes for a degenerate portion of the grammar i've an empty visitor that
always throw an exception,
the equivalent code with a switch is
static Object result(Object o) {
return switch (o) {
case Object __ -> throw new AssertionError();
};
}
Obviously i can tweak the code generator to generate
static Object result(Object o) {
throw new AssertionError();
}
but not be able to compile the former code strike me as odd.
An expression switch is a poly-expression, so the result type is
back-propagated from the return type of the method result, so it should be
Object.
Moreover, if the switch is not a switch expression but a switch statement, the
code is also valid
static Object result(Object o) {
switch (o) {
case Object __ -> throw new AssertionError();
}
}
Not be able to compile a switch expression when there is no explicit result
type but only an implicit type seems arbitrary to me
(this change is backward compatible because it only makes more codes compiling).
Rémi