JEP 468 proposes 'derived record creation' (with expressions or withers) to reduce verbosity when working with immutable Java records. Currently, creating modified copies of records requires manual boilerplate wither methods or verbose constructor calls. The proposed `with` expression lets you write `Point updated = old with { x = 0; };`, where Java automatically copies unchanged components from the origin record, runs the transformation block, and calls the canonical constructor with the resulting values. This ensures constraints are always checked, supports nested expressions, and mirrors the minimalism of compact record constructors. The feature was a candidate JEP targeting potentially JDK 23. The post also clarifies that while `with` expressions can simulate named parameters via a default instance pattern, they are not true named parameters.

10m read timeFrom nipafx.dev
Post cover image
Table of contents
▚ Intro▚ When Immutability Births Verbosity▚ Derived Record Creation▚ Nitty Gritty▚ Named Parameters - Not!

Sort: