[pattern-switch] Opting into totality
Brian Goetz
brian.goetz at oracle.com
Tue Sep 1 14:09:52 UTC 2020
> It's unclear whether your "some remainder" is allowed to be empty. (There was some discussion earlier about outlawing 'default' in the equivalent of a sealed switch.) I hope full totality is fine—an expression switch, implicitly 'sealed', of course permits a 'default' clause.
Yes, remainder can be empty. The key aspect of sealed switches is that
we generate code to handle the remainder. Of course, it could be
handled explicitly too, in which case the synthetic handler is never
matched. For example:
sealed switch (foo ) {
case Foo(var x): ...
}
is total on Foo with remainder { null }. So sealing will cause us to
throw on null. OTOH, you could also say:
sealed switch (foo ) {
case null: haha(); break;
case Foo(var x): ...
}
The compiler does not need to analyze the cases that might match
remainder; it just generates code at the end of the switch so that, if a
previous case doesn't cover it, the catch-all does.
> And then note that, given the existence of 'sealed switch', the 'default Object o' feature is redundant. If you want to make sure you have a total case in your switch, just say 'sealed' at the top. All sealed switches (both statement and expression) guarantee either optimistic totality + NPE or that the last clause is total.
Yes, that's right. If there's a way to ask for totality, you can
safely use a total pattern without `default`. So `default`, in this
model, reverts to being `case _`, which is a fine role for default in
this new world.
More information about the amber-spec-experts
mailing list