Randgalt / record-builder

Record builder generator for Java records
Apache License 2.0
723 stars 51 forks source link

Setting an optional value should not require wrapping in "Optional" #184

Closed koppor closed 2 months ago

koppor commented 2 months ago

At https://github.com/Randgalt/record-builder/commit/62011fe2b395fef52421b7d9793cce7612fdc3b9#diff-9894c8ca988dd9ba18c16d1cc0176ca25e915f1b212f12e9c5f285d44fa10e72R81, the value of the example optional Property "b" was made using .b(Optional.of("bbbb"). I wonder, why one could not simply write .b("bbbb")? The method b can create the Optional by itself. - IMHO the code would be more readable.

Randgalt commented 2 months ago

You can get that behavior by using addConcreteSettersForOptional = true. E.g.,

@RecordBuilder
@RecordBuilder.Options(builderMode = RecordBuilder.BuilderMode.STAGED_REQUIRED_ONLY, interpretNotNulls = true, useImmutableCollections = true, addConcreteSettersForOptional = true)
public record OptionalListStaged(int a, Optional<String> b, double c, List<Instant> d, @Null String e, String f) {
}
koppor commented 2 months ago

Thank you for the quick reply! 🤩 (I was about to comment after reading through https://github.com/Randgalt/record-builder/blob/master/options.md)