Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested buildable types should use the enclosing type's defaults #437

Open
alicederyn opened this issue Nov 9, 2020 · 0 comments
Open

Nested buildable types should use the enclosing type's defaults #437

alicederyn opened this issue Nov 9, 2020 · 0 comments
Labels

Comments

@alicederyn
Copy link
Collaborator

alicederyn commented Nov 9, 2020

Default values set on a nested buildable type in the constructor should be ignored when merging, but unfortunately they are currently treated as explicitly-set values, as the merge method in the nested mergeFrom method uses the defaults from its own constructor.

e.g.

@FreeBuilder
interface TypeA {
  int value();  // No default
  class Builder extends TypeA_Builder { }
}

@FreeBuilder
interface TypeB {
  TypeA a();  // Value defaults to 0 on this type
  class Builder extends TypeB_Builder {
    Builder() {
      super().mutateA(a -> a.value(0));
    }
  }
}

TypeB.Builder x = new TypeB.Builder();
TypeB.Builder b = new TypeB.Builder();
b.a().value(10);
b.mergeFrom(x);  // This should not overwrite a.value
y = b.build();
assertEquals(10, y.a()); // Fails

Not sure how to easily fix this — would probably require an API extension, to have the option of passing a different default into the mergeFrom algorithm. It would also break existing code, so probably needs to be a major version bump when fixed.

@alicederyn alicederyn added the bug label Nov 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant