Updated document on data classes and sealed types

Brian Goetz brian.goetz at oracle.com
Sat Mar 2 13:30:14 UTC 2019

> but can I compatibly turn a class type into a record type? It's 
> potentially a source-compatible change (make the state description 
> match the class's ctor), but binary-compatible?

Yes.  You can transform

     class Point {
         final int x, y;

         public Point(int x, int y) { this.x = x; this.y = y; }

         public int x() { return x; }
         public int y() { return y; }

         // state-based equals and hashCode

         // more methods


     record Point(int x, int y) {
         // more methods

and this will be source- and binary-compatible.

> Similarly, if a record type is chafing at the restrictions of "state 
> only!", then can I (source|binary)-compatibly turn it into a class type?

Yes, mostly.  Records do currently have one aspect that is not *yet* 
denotable by ordinary classes; the pattern match extractor.  Until 
ordinary classes can declare one, existing records whose clients use 
pattern matching can't be migrated.

More information about the amber-spec-observers mailing list