Mark Reinhold announced two new closely related candidate JDK Enhancement Proposals (JEPs) on the OpenJDK amber-dev mailing list this week with the posts "New candidate JEP: 359: Records (Preview)" and "New candidate JEP: 360: Sealed Types (Preview)." Both of these candidate JEPs are "preview features" (defined by JEP 12).
JEP 359: Records (Preview)
The JEP 359 "Summary" states, "Enhance the Java programming language with records. Records provide a compact syntax for declaring classes which are transparent holders for shallowly immutable data."
The "Motivations and Goals" section of JEP 359 explains how records would benefit Java developers. That section begins by stating that "it is a common complaint that 'Java is too verbose' or has too much 'ceremony" and explaining that "some of the worst offenders are classes that are nothing more than plain 'data carriers' that serve as simple aggregates." This section also states that records are intended to be more than mere "boilerplate reduction" and that they "should be easy, clear, and concise to declare shallowly-immutable, well-behaved nominal data aggregates." In short, the stated driving goal of JEP 359 is to "model data as data."
The recently proposed java.lang.Record
draft specification provides significant insight into the characteristics of records. The opening paragraph of the "Description" section of JEP 359 also describes records: "Records are a new kind of type declaration in the Java language. Like an enum, a record is a restricted form of class. It declares its representation, and commits to an API that matches that representation. Records give up a freedom that classes usually enjoy: the ability to decouple API from representation. In return, records gain a significant degree of concision." There is significantly more text in the "Description" section of JEP 359.
JEP 360: Sealed Types (Preview)
The "Summary" section of JEP 360 states, "Enhance the Java programming language with sealed types. Sealed types are classes or interfaces that impose restrictions on which other classes or interfaces may extend or implement them."
The "Goals" section of JEP 360 is also concise, "Enable classes and interfaces to limit permitted subtypes to an enumerated set of types in the same maintenance domain as the type itself."
It is the "Description" section of JEP 360 that provides concreteness to JEP 360. That section begins, "A sealed type is one for which subtyping is restricted according to guidance specified with the type's declaration." The second paragraph of the "Description" section states that "sealing serves two distinct purposes" and describes those purposes:
- "Restricts which classes may be a subclass of a sealed class."
- "Potentially enables exhaustiveness analysis at the use site, such as when switching over type patterns for an instance of a sealed type."
There are other interesting characteristics of sealed types described in the "Description" section. Some of these that stood out to me are:
- Use (with example) of the
sealed
modifier andpermits
clause. - "Abstract subtypes of sealed types are implicitly sealed, unless declared with the
non-sealed
modifier." - "Concrete subtypes of sealed types are implicitly final, unless declared with the
non-sealed
modifier." - "Sealing, like finality, is enforced by both the language compiler and by the JVM. The sealed-ness of a type, and its list of permitted subtypes, are reified in the class file and enforced at runtime."
Other interesting details related to sealed types that are covered in this JEP include restrictions (compiler errors that can occur), the class form for sealed types, and addition of reflection methods to support sealed types.
Conclusion
JEP 359 (Records Preview) and JEP 360 (Sealed Types Preview) reference each other in their documentation. Of the relationship between these two candidate JEPs, JEP 360 states, "Sealed types and records, taken together, form a construct often referred to as algebraic data types." Records and sealed types are key pieces in the move toward Java support for pattern matching.
No comments:
Post a Comment