with and binary backward compatibility
Brian Goetz
brian.goetz at oracle.com
Tue Jun 14 17:32:38 UTC 2022
> The problem is that if we run that code with the new version of Point (the one with 3 components),
> newPoint.z is not equals to point.z but to 0, so once there is a 'with' somewhere, there is no backward compatibility anymore.
Yes, in that case, we experience something like "decapitation" of the z
value. But note the same thing is true without the `with` mechanism!
// Old code
record Point(int x, int y) { }
class Foo {
Point increment(Point p) {
return new Point(p.x() + 1, p.y() + 1);
}
}
// modify Point and recompile Point but not Foo
record Point(int x, int y, int z) {
Point(int x, int y) { this(x,y,0); }
}
// new code
Point p = new Point(1,2,3);
System.out.println(foo.inc(p)); // Point[2,3,0]
This has nothing to do with "with"; without with, we have the same
"brittle" constructor/deconstructor selection. With "with", we have the
same, it's just not as obvious. Actually, it's better, because then
its only a separate compilation artifact; if you recompile the
with-using client, things are righted, whereas with the explicit
ctor-and-accessor version, it is never righted. So `with` is actually a
compatibility improvement here, in that the brittle selection goes away
after recompilation.
More information about the amber-spec-experts
mailing list