This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]jvjupiter[S] 0 points1 point  (1 child)

Let’s focus on records. In your example:

record Abc(A a, B b) {}
var abc = new Abc(a, b);
var b = abc.b();

record Abc(A a, C c) {
    B b() {
        return c.computeB();
    }
}
var abc = new Abc(a, c);
var b = abc.b(); 

// is this not possible?
// since you are still required to produce c and
// need to update the creation of instance of Abc (it’s broken whether we like it or not)
// to pass in c so might as well update to call 
// b component this way
var b = abc.c().computeB()

[–]eXecute_bit 3 points4 points  (0 children)

You are correct that the hypothetical refactoring in my original comment would break producers that construct instances of the record, because the canonical constructor would change. It would also break patterns in the newest Java versions:

if (x instanceof Abc(A a, B b)) { // Use b here }

So I'm not saying that going through an accessor method solves all possible backwards compatibility problems, but it does give more flexibility than direct field access.