Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

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 subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

1 participant