Randgalt / record-builder

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

Default values and staged builders #187

Open CfFanDuel opened 4 weeks ago

CfFanDuel commented 4 weeks ago

Staged builders are useful to force compile time safety that all required attributes are set. The autocomplete they give is lovely too.

If any components are marked as nullable then they are optional.

The same isn't true for components marked with a default value. You're forced to specify a value. This is wrong I feel.

Sample code to see:

@MyStagedRecordBuilder
public record Person(
        @Initializer("DEFAULT_NAME") String name,
        @Nullable int age,
        String somethingElse
) {

    public static final String DEFAULT_NAME = "John Doe";

    void test() {
        var x = PersonBuilder.staged()
                .name("ds")
                .somethingElse("aada")
                .age(21);
    }
}

In this instance as name has a default value the only component I should be forced to specify is somethingElse, but i'm forced to specify name.

I'm conscious at this point this is trying to turn the library into a replacement for named and default arguments found in other languages. But seeing as both of these pieces of functionality are available I thought it worth raising :)