Skip to content

Optional with default#1582

Merged
elucash merged 2 commits intoimmutables:masterfrom
hwellmann:optional-with-default
Aug 9, 2025
Merged

Optional with default#1582
elucash merged 2 commits intoimmutables:masterfrom
hwellmann:optional-with-default

Conversation

@hwellmann
Copy link
Copy Markdown
Contributor

Handles Optionals with default value, adds setter and wither methods with the unwrapped type and no longer prints warnings for this case.

Fixes #1432

@elucash
Copy link
Copy Markdown
Member

elucash commented Aug 8, 2025

Thank you for PR! I definitely welcome this functionality, but aside from some refinements I'll have to do (make sure nullability annotations added for when optional accepts nullable etc.), I need to check if this would work correctly in general.
To be clear, instead of warning, we introduce a new notion of isOptionalWithDefault. In all behaviors it will be considered as REGULAR attribute having a default (isGenerateDefault), but we use isOptionalWithDefault to add some convenience. In the light of this, I still feel like warning is not that bad as it still would remind of that this will not act as optional, but as regular with default (+some conveniences). It's not that we need to get warning back, but definitely need to make sure the documentation, at least, tries to explain that. We have a paragraph about that I guess, but we might need to revise it (or add if it's not there).
In any case, I would as if you can, for example add/revise a note to the Value.Default Javadoc explaining what will happen if Default is placed on the Optional attribute: i.e. it will lose optional status, but will retain some conveniences like initializing from the unwrapped value (like next PR if this will be merged by then)

@elucash elucash merged commit cc5230f into immutables:master Aug 9, 2025
16 checks passed
@elucash elucash added this to the 2.11.3 milestone Aug 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generate two setter for default Optional value

2 participants