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

Empty collections by default using RecordInterface #152

Open
JanecekPetr opened this issue Apr 5, 2023 · 4 comments
Open

Empty collections by default using RecordInterface #152

JanecekPetr opened this issue Apr 5, 2023 · 4 comments
Labels
enhancement New feature or request good first issue Good for newcomers PR welcome A PR submitted for this issue would be welcome

Comments

@JanecekPetr
Copy link

JanecekPetr commented Apr 5, 2023

My goal: Never to get a null from any Collection-like component.

Currently there's the useImmutableCollections option for this, however that's not perfect. First of all, it does not work with SortedSet / NavigableSet / SortedMap / NavigableMap, but also e.g. Guava's collections types (#151). Also the record's canonical constructor can still be used directly, circumventing record-builder, and that makes any guarantees shaky. One more problem: collection copying has a cost, and our team actually decided to not use useImmutableCollections by default, so in our case it would not help even it all the previous reasons were void.

One can use the Validation API, but again that can be circumvented.

Therefore, the only way to make this work all the time is to manually write checks:

record Car(List<String> passengers) {

    Car {
        if (passengers == null) {
            passengers = List.of();
        }
    }

}

This of course works, but it's a relatively big piece of manual code that has to be in every record.

Would be nice if record-builder did this out-of-the box for @RecordInterface-generated records. No more nulls, ever!

@Randgalt Randgalt added PR welcome A PR submitted for this issue would be welcome good first issue Good for newcomers labels Apr 11, 2023
@Randgalt
Copy link
Owner

I was working on something similar with this: #120 - but that was way more experimental.

@Randgalt Randgalt added the enhancement New feature or request label Apr 11, 2023
@pawellabaj
Copy link
Contributor

pawellabaj commented Jul 4, 2023

@JanecekPetr I work on a library we using in our projects to generate records based on an interface: auto-record.

It generates boiler plate code for a nullability checking in a compact constructor (we use nonNull by deafult approach), see examples at https://github.com/pawellabaj/auto-record/wiki/Nullability

I think generating empty collections in a compact constructor could be added as an option.

@Anupam2528
Copy link

Hey @JanecekPetr / @Randgalt
are we trying to implement emptyInNull kind of scenario here?
could you please let me know?

@Randgalt
Copy link
Owner

Randgalt commented Aug 2, 2023

I'm open to PRs on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers PR welcome A PR submitted for this issue would be welcome
Projects
None yet
Development

No branches or pull requests

4 participants