Behavior of Switch Statements without fallthrough
Brian Goetz
brian.goetz at oracle.com
Tue Jul 22 16:06:12 UTC 2025
> As it says in JEP 361: The greater the divergence between switch
> expressions and switch statements, the more complex the language is to
> learn, and the more sharp edges there are for developers to cut
> themselves on.
We would love to give the same treatment to all switches; this is
simpler and more uniform, which benefits almost everyone. We can't get
there immediately, but we can get there, but not without some small pain
for someone.
Right now, we have "all switches except legacy switches" (where legacy
is the intersection of "doesn't use patterns, is a statement, doesn't
use guards, etc") are exhaustive. But it would surely be better to just
get to "switches are exhaustive". To do that, though, some existing
code will stop compiling. Fixing such code is "easy" (just add a
do-nothing default), but it is a source-incompatible change. The last
time we did this was Java 9, where we reclaimed _ from the space of
identifiers; we had to do go through a "deprecation" process which
started with warnings, then suppressible errors, then errors. And this
consumes a finite tolerance that the ecosystem has for "forcing them to
make changes". So its possible, but to be approached carefully.
More information about the amber-dev
mailing list