JEP 405 proposes record patterns as a preview feature in JDK 19, enabling developers to deconstruct records directly within pattern matching switch expressions. By placing a record's canonical constructor syntax in a case label (e.g., `case OrderEvent(Instant timestamp, Order order)`), all type-checking and value extraction moves to the left-hand side, leaving only processing logic on the right. Record patterns can be nested for deeper deconstruction and support named patterns to retain a reference to the whole record. Components are matched by position, not name, and `var` can be used to skip explicit types. The post also speculates on future additions like underscore wildcards for unused components and destructuring assignments outside of switch.

7m read timeFrom nipafx.dev
Post cover image
Table of contents
▚ Intro▚ Deconstructing Records in Pattern Matching▚ Speculations▚ Outro

Sort: