Randgalt/record-builder

Collection setter signature depends on addSingleItemCollectionBuilders option

freelon opened this issue · 8 comments

@RecordBuilder
public record SomeRecord(List<Integer> l) {
}

generates public SomeRecordBuilder l(List<Integer> l) {

while adding

@RecordBuilder.Options(
    addSingleItemCollectionBuilders = true
)

changes it to public SomeRecordBuilder l(Collection<? extends Integer> l) {.

Damn - this is hard to fix without breakage. It needs some thought.

I wonder if we just decide to change the other one to Collection - I don't think it will cause much disruption.

I wouldn't think so, either.
A check would be needed whether the parameter is in fact the specified type, e.g. List, and if so, just store it, otherwise create a copy of it. The question would be what type the copy should be of, though. An immutable list or just ArrayList?

@freelon I lost track of this. Does the PR I just merged assert the change we discussed?

@Randgalt I'm forgot myself, but I don't think so. I'll check later, but I can provide another MR for it.

I was thinking about this again, and I think it's a bit counterintuitive to have a parameter of type Collection<T> in the setter. It would just be a special case for Set<T> and List<T> and as I mentioned above, the question would be of which type an internally allocated list or set should be, if the parameter was of some other collection type.
I did a quick search on GitHub for usages of the setter-methods where a non-list/non-set collection was used and I didn't find any (generally I didn't find a usage of the addSingleItemCollectionBuilders flag outside this project). So I think the breaking change from Collection<T> to List/Set<T> for that parameter would be fine.

My concern is does this limit utility? List is a type of Collection and people can create custom classes that implement Collection but not List/Set. Thoughts?

Well in that case they could just define a property in the record as type Collection<T>, then they would have the setter with parameter type Collection. Or am I misunderstanding what you mean?