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

Compiler implementation of Pattern Matching for instanceof (Final)

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 16
    • tools
    • None
    • source
    • minimal
    • Two reflective preview methods in BindingPatternTree have been removed and replaced with a new method. As a preview API, the users of the API were warned, and should be prepared to update their code.
    • Java API, Language construct
    • SE

      Summary

      The Java language feature "pattern matching for instanceof", currently a second round of preview in JDK 15, is to be made final and permanent in JDK 16. Several, small adjustments to the feature are proposed.

      Problem

      The proposed Java language feature "pattern matching for instanceof" has been through two rounds of preview, which has resulted in some feedback. Adjustments based on the feedback should be made to the feature, and it should be made final and permanent.

      Solution

      The following changes have been made to the design of the feature and are taken from the JEP (394):

      • Lift the restriction that pattern variables are implicitly final, to reduce asymmetries between local variables and pattern variables.

      • Make it a compile-time error for a pattern instanceof expression to compare an expression of type S against a pattern of type T, where S is a subtype of T. (This instanceof expression will always succeed and is then pointless. The opposite case, where a pattern match will always fail, is already a compile-time error.)

      The following changes address implementation improvements:

      • The Trees API has been modified so that BindingPatternTree contains a VariableTree that declares the actual binding variable. This is to improve consistency of the API.

      • The feature is made non-preview, including removing preview warnings from modelling APIs.

      Specification

      The draft specification is attached and is also available for convenience here. The specification draft enumerates the more significant changes in the introduction, which can be summarized as follows:

      • To support non-final binding variables: changes primarily to sections 4.11 and 4.12.3, and also a slight change to the grammar for type test patterns in section 14.30.1.1.
      • To ensure the compile-time error for expression type being a subtype of the pattern type: changes to section 15.20.2
      • Some refactoring of the description of the semantics of pattern instanceof: changes to sections 15.20.2 and 14.30.3

      The specdiff of the API adjustment is attached and is also available for convenience here: http://cr.openjdk.java.net/~jlahoda/8250625/specdiff.01/overview-summary.html.

        1. jep394-20201211.zip
          39 kB
        2. jep394-jls.00.zip
          42 kB
        3. jep394-jls.01.zip
          42 kB
        4. jep394-jls.02.zip
          37 kB
        5. patterns-spec-review.diff
          46 kB
        6. specdiff.00.zip
          3.33 MB
        7. specdiff.01.zip
          3.34 MB

            jlahoda Jan Lahoda
            gbierman Gavin Bierman
            Vicente Arturo Romero Zaldivar
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: