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

Compiler Implementation for Derived Record Creation (Preview)

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • tools
    • None
    • source
    • minimal
    • Language construct
    • SE

      Summary

      Enhance the Java language with derived creation for records. Records are immutable objects, so developers frequently create new records from old records to model new data. Derived creation streamlines code by deriving a new record from an existing record, specifying only the components that are different.

      Problem

      Record types are, by their specification, intentionally immutable. That makes them safer, and makes it easier to use them for multi-threaded code. But, when a value of a record component needs to change, the whole record must be recreated, copying the unmodified components to the new record, and replacing the modified components with new values. Such a process elaborate and error-prone.

      Solution

      The solution proposed herein is to introduce a new expression that allows to easily create a new record type instance, based on an existing record type instance: the Derived Record Creation Expression. This expressions allows to change the values of component that should be changed, while automatically copying values of components that are unchanged. For example:

      record R(String component1, Integer component2) {}
      R r = new R("Hello, ", 42);
      r = r with {
          component1 = "World!"; //altering the value of the component1
          //but doing nothing for component2
      };
      assert r.equals(new R("World!", 42));

      In particular the solution is to:

      • enhance the Java language with the Derived Record Creation Expression
      • enhance the Trees API with a new kind of the AST nodes, representing the newly introduced expression, and corresponding changes across the API
      • enhance the Java Language Model with a new ElementKind.COMPONENT_LOCAL_VARIABLE for the newly defined type of variables inside the Derived Record Creation Expression, and related changes. These are covered by a separate CSR: https://bugs.openjdk.org/browse/JDK-8329645.

      Specification

      The draft JLS changes is attached and also available at: https://cr.openjdk.org/~gbierman/jep468/latest/

      The draft specdiff for the API changes is attached as specdiff.preliminary.01.zip, and also available for convenience here: https://cr.openjdk.org/~jlahoda/8324651/specdiff.preliminary.01/overview-summary.html

      Please note the specdiff also includes changes for https://bugs.openjdk.org/browse/JDK-8329645.

      This is a preview language feature.

      Both these drafts are subject of change until the CSR is Finalized.

            jlahoda Jan Lahoda
            gbierman Gavin Bierman
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: