Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8207241

Compiler support for Switch Expressions (Preview)

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P3 P3
    • 12
    • tools
    • None
    • source
    • minimal
    • Hide
      The addition of the feature does not by itself bring a compatibility risk.

      Switch expressions are a preview feature in Java SE 12. It is possible that incompatible changes will be made to switch expressions in a later Java SE release, before they become final and permanent. It is also possible that switch expressions will be removed in a later Java SE release, without ever having become final and permanent.
      Show
      The addition of the feature does not by itself bring a compatibility risk. Switch expressions are a preview feature in Java SE 12. It is possible that incompatible changes will be made to switch expressions in a later Java SE release, before they become final and permanent. It is also possible that switch expressions will be removed in a later Java SE release, without ever having become final and permanent.
    • Language construct
    • SE

      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) 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.

            jlahoda Jan Lahoda
            jlahoda Jan Lahoda
            Alex Buckley
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: