Java proposal would streamline record creation

Derived record creation would allow developers to create a new record from an existing record, updating only the values that need to change.

3 replication bulldogs twin
Thinkstock

In an effort to streamline code, Java would be enhanced with derived record creation, via a proposal that would provide a means to create record instances from existing records through a block of transformation code.

The OpenJDK proposal authored by Oracle technical staff, titled “Derived Record Creation,” would streamlinescode by deriving a new record from an existing record, updating only components that need to change. The proposal notes that because records are immutable values, developers frequently build new records to reflect new data. The feature would enter Java in a preview state.

Goals of the proposal include providing a concise means to create new record instances derived from existing record values, and streamlining the declaration of record classes by eliminating the need to provide explicit “wither” methods. Wither methods are similar to setter methods but for immutable classes.

It is not a goal, the proposal states, to support a distinguished class of wither methods or to provide a Pascal-style with construct that simplifies access to arbitrary complex expressions. It also is not a goal to provide derived instance creation expressions for ordinary, non-record class values, although that may be the subject of a future JEP (JDK Enhancement Proposal).

In explaining the motivation for derived record creation, the proposal notes that the immutability of record classes gives both safety and predictability, and enables a number of features that make them easy to use. However, the systems that developers need to model have state, and evolving state modeled by record classes can be cumbersome. Derived creation provides a succinct way to update the components of record classes when the underlying state changes.

Derived record creation would be part of the Standard Edition (SE) of Java. No specific version of Java SE is cited in the proposal yet as a target for the capability. The current version of standard Java is JDK 21, with successor JDK 22 due in March, with its feature set already frozen. JDK 23 is expected in September.

Copyright © 2024 IDG Communications, Inc.