JDK-8275408 : Pattern Matching for switch (Second Preview)
  • Type: CSR
  • Component: tools
  • Sub-Component: javac
  • Priority: P4
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 18
  • Submitted: 2021-10-18
  • Updated: 2022-05-23
  • Resolved: 2021-11-18
Related Reports
CSR :  
Relates :  
Relates :  
Description
Summary
-------

The pattern matching for switch feature should continue to be a preview feature for JDK 18, having been adjusted based on the feedback from being a preview feature in JDK 17.

Problem
-------

More time is needed to gain experience with the pattern matching for switch feature. Following feedback from the first preview some small amendments have been make, and feedback on these amendments and the feature in general should be gathered.

Solution
--------

The pattern matching for switch preview feature will continue to be a preview feature for JDK 18. The spec and compiler will be adjusted based on the current feedback.

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

The updated JLS for pattern matching for switch is attached, and is also available for convenience here:
http://cr.openjdk.java.net/~gbierman/jep420/jep420-20211111/specs/patterns-switch-jls.html

The current significant changes to the spec from the first preview are:

 * the any pattern (including an any pattern that is guarded) dominates a constant pattern of the same type
 * permitted types that can't be cast to the selector type of the switch statement or expression (and hence a `case` cannot be written for them) are ignored when checking exhaustiveness of a switch
 * the specification of the resolution of a switch block has been re-written to be more readable. This is not a semantics altering change, but rather a significant editorial change

Currently no API changes.

The changes to the specification and API are a subject of change until the CSR is finalized.
Comments
Thanks [~gbierman]; moving to Approved.
18-11-2021

Thanks [~darcy]. Strictly speaking this sort of thing isn't binary compatibility in the sense of JLS Chapter 13. However, we have decided that some words here would be useful. I have attached a new spec and also a diff file so you can see exactly what has been added. I have also taken this opportunity to clarify some of the wording around exhaustiveness - there is no change to semantics.
11-11-2021

Are there any binary compatibility concerns (JLS chapter 13) or separate compilation issues that should be called out as part of this work by virtue of the new semantics of pattern matching? In other words, "be careful doing X now since X can break compiled pattern match switches". Moving to Provisional.
26-10-2021