JDK-8240999 : Implement javac changes for deconstruction patterns.
  • Type: CSR
  • Component: tools
  • Sub-Component: javac
  • Priority: P3
  • Status: Draft
  • Resolution: Unresolved
  • Submitted: 2020-03-13
  • Updated: 2020-04-16
Related Reports
Relates :  
Description
Summary
-------

Under JEP 305 (https://bugs.openjdk.java.net/browse/JDK-8181287), pattern matching for instanceof has been added as a preview feature, together with a support for type test patterns, like:

    ... o is a variable declared with a general type such as Object ...
    if (o instanceof <type> v) {
        ... use the newly declared variable v
            which refers to the same object as o
            but with a more specific type ...
    }

Herein, we propose to a) preserve the pattern matching in instanceof feature, and the type test patterns, as a preview for JDK 15; b) add a new type of patterns, the deconstruction patterns.

The deconstruction patterns will allow to split record types into their components, like:

    ... o is a variable declared with a general type such as Object ...
    if (o instanceof <record-type>(var component1, var component2)) {
        ... use the newly declared variables component1 and component2
            which refer to the components of <record-type>
    }

Problem
-------

Access to record components using accessors may be inconvenient/verbose in some cases. Being able to dismantle the record in one step to components will be useful.

Solution
--------

The proposed changes are twofold:
a) the current preview feature - pattern matching in instanceof and type test patterns will remain a preview feature in JDK 15
b) a new kind of patterns is added - the deconstruction patterns. These will allow to dismantle record into their components in one step. This will be part of the preview feature as well.

As a corollary of the implicit nature of the deconstruction patterns for records, it will currently be at least a source-incompatible change to add a new or remove existing state component to or from a record. The pre-existing deconstruction patterns in the source code will then use an incorrect number of components, and will not compile. Reordering state components will face similar issues.

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

The current draft of the JLS change, and the diff for the corresponding API changes are attached. For convenience, the JLS draft can also be found here: http://cr.openjdk.java.net/~gbierman/jep375/jep375-20200316/specs/patterns-instanceof-jls.html

Comments
A new CSR for the JDK15 work is here: https://bugs.openjdk.java.net/browse/JDK-8242368
16-04-2020

The deconstruction patterns have been withdrawn from JDK 15, so returning back to draft. A new specification draft might be proposed for some other JDK release.
16-04-2020

Moving to Provisional. It would ease review of the Finalized version of the request if the JEP 375 specific changes were clearly identified.
19-03-2020

[~gbierman], what is the base document for the JLS changes? The baseline does *not* look to include the initial record and pattern support from previous JEPs; thanks.
19-03-2020