JEP 405 Record Patterns and Backwards Compatible Changes
Nathan Walker
nathan.h.walker at gmail.com
Sun Jun 12 00:39:22 UTC 2022
Currently in Java 18 we can make backwards compatible changes to the
contents of a record.
Given:
record Point(int x, int y){ }
We can add point z with:
record Point(int x, int y, int z) {
@Deprecated
Point(int x, int y){ this(x, y, 0); }
}
Or given:
record Point(int x, int y, int z){ }
We can remove point z with:
record Point(int x, int y) {
@Deprecated
Point(int x, int y, int z){ this(x, y); }
@Deprecated
public z(){ return 0; }
}
If they are marked serializable then even the serialized forms maintain
backwards compatibility, with the z value in the first example being
defaulted to 0 during deserialization, and the z in the second example
being ignored during deserialization.
But if I understood JEP 405 correctly, a record pattern depends on matching
the exact record components. So is there a way in JEP 405 to change a
record's components without breaking everywhere that is using the record in
a pattern match statement? I would be concerned about using records in any
public API if every minor change to a record's structure will require a new
major version release under semantic versioning rules.
Thanks for your time.
More information about the amber-dev
mailing list