JDK-8207241 : Compiler support for Switch Expressions (Preview)
  • Type: CSR
  • Component: tools
  • Sub-Component: javac
  • Priority: P3
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 12
  • Submitted: 2018-07-13
  • Updated: 2019-06-13
  • Resolved: 2018-08-08
Related Reports
CSR :  
Relates :  
Relates :  
Relates :  
Description
Summary
-------

Extend the switch statement so that it can be used as either a statement or an expression, and so that both forms can use either a traditional `case :` label (with fall through) or a simplified `case ->` label (no fall through). 

Problem
-------

Several irregularities of the switch statement have long been an irritation to users, and impediments to support for features like pattern matching. These irregularities include (i) the default control flow behavior (fall through) of switch blocks, (ii) the default scoping of switch blocks (the block is treated as one single scope), and (iii) the fact that switch works only as a statement, even though it is commonly more natural to express multi-way conditionals as expressions.

Solution
--------

The JLS will be enhanced with support for:

 - `switch` expressions
 - in `switch` expressions and `switch` statements, a choice between switch labeled rules (`case ->`) and switch labeled statement groups (`case :`).
 - in `switch` expressions and `switch` statements, the ability to have multiple constants in a case label.
 - in `switch` expressions, the introduction of a _value `break` statement_ to yield a result from the switch expression.

These features, as a group, will constitute a _preview feature_ ([JEP 12](http://openjdk.java.net/jeps/12)) in Java SE 12.

Specification
-------------

Proposed changes to the JLS are attached to this CSR. The changes are also available at http://cr.openjdk.java.net/~gbierman/switch-expressions-2019-01.html. There are no changes to the JVMS.


Comments
Thanks for the clarification. The changing typing rules for ?: colon over the releases have been the source of some minor puzzlers. Moving to Approved.
08-08-2018

Thanks Joe. The relationship is, as they say, a little complicated. The fundamental property is that the typing rules are the same - actually our typing rules are an n-ary version of the more verbose tables in JLS 15.25. (We decided not to streamline those for 15.25, but we well might in another release.) The difference is in the rules for categorization of conditional expressions as standalone/poly are different than the rules for categorization of switch expressions. There exist conditional expressions (that operate on booleans or numbers) that will be treated as standalone and have different behavior than the equivalent (poly) switch expression. For example: int i = 3; double d = 0.0; Double val1 = test ? i : d; // allowed Double val2 = switch (test) { case true -> i; case false -> d; }; // error: int->Double illegal The sense after discussions was that the complexity of ?: categorization was not what we wanted to carry over to switch expressions. Moreover, we didn't want to make any changes to ?: categorization to make it align to switch expressions as part of this release. That would be more than spec cleanup, but actually a breaking change to the language. It's a corner-case, but we'd need to assess the impact carefully. So, the current proposal is that a conditional expression can be refactored as a switch expression, or vice versa, without risking a change to the expression's type, if one of the following is true: - The expression is not in an assignment/invocation context - The operands are not all primitive/boxed booleans and are not all primitive/boxed numbers - When treated as a standalone expression, the expression's type is the same as the target type The negation of this feels like a very rare situation. Hope this clarifies.
30-07-2018

Moving to Provisional. Over the releases, there are been adjustments to how the type of a ?: expression is calculated. Is that calculation consistent with the typing rules of switch expressions?
23-07-2018