I've noticed that the public API of JRE23 doesn't include record classes, with all their usage being found in internal packages like `com.sun`. It seems like records might not be good for situations where maintaining backward compatibility is necessary. Can anyone explain why that is?
1 Answer
Actually, there are records in the JRE23 API, like `UnixDomainPrincipal` and other classes. But your point stands about backward compatibility. Records are generally more rigid than traditional classes. Once you define a record, changing its fields can break existing code that depends on that structure. So, it’s wise to avoid using them in public APIs unless you’re sure your design will remain stable for the long haul.
Right! Adding a new field to a record can easily break code that destructures it, which is a real concern for developers. Once we get more features, like deconstruction patterns, that could help with compatibility, but removing fields or changing types is still a big no-no.