New JEP: Switch Expressions for the Java Language
Stephen Colebourne
scolebourne at joda.org
Tue Dec 12 11:29:43 UTC 2017
On 12 December 2017 at 01:12, Brian Goetz <brian.goetz at oracle.com> wrote:
> two similar but subtly different kinds of switch statements.
Reading all the mails on this topic, it sure looks like the statement
switch for pattern matching is going to have a different set of
rules/limitations from the current statement switch, such as "default
case must be last" and "cases have an order". There comes a point at
which jumping through hoops to keep the old thing working isn't
sensible. My view is that these different rules would be a lot clearer
with a different syntax (eg switch... when, instead of switch ...
case), even if you wait until pattern matching before you add it.
One final thing. If you are determined to keep the existing statement
switch and expand use of it, the relatively simple job of adding a
keyword such as "fallthrough" would make a huge difference to those
who passionately avoid switch today. This would replace the comments
currently used by tools like checkstyle/spotbugs:
http://checkstyle.sourceforge.net/config_coding.html#FallThrough
http://spotbugs.readthedocs.io/en/latest/bugDescriptions.html#sf-switch-statement-found-where-one-case-falls-through-to-the-next-case-sf-switch-fallthrough
A javac warning could be added to ensure that each case clause has
either "break" or "fallthrough" (and IDEs would have a setting to make
the warning into an error).
(For anyone thinking of suggesting "continue" instead of
"fallthrough", it doesn't work. "continue" binds to the surrounding
loop when inside a switch statement. #facepalm)
Stephen
More information about the amber-dev
mailing list