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

Feature request: Generate update(UnaryOperator)-methods along withers (new style flag?) #1408

Closed
ajkettun opened this issue Oct 10, 2022 · 3 comments · Fixed by #1409
Closed
Milestone

Comments

@ajkettun
Copy link

ajkettun commented Oct 10, 2022

Currently transforming Immutables-generated objects requires to write rather clumsy code.

immutableBar.withFoo(doSomeTransformation(immutableBar.getFoo()))

And it gets worse with nested objects.

Simple improvement could be if Immutables would generate update-methods along with withers to allow transforming nested objects more cleanly. Basic update-method signature for a String foo-property in ImmutableBar -class would be:

ImmutableBar updateFoo(UnaryFunction<String> updater);

Which would allow to write:

immutableBar.updateFoo(foo -> ...)

Or if the foo-property would be another immutables -generated object instead of String:

immutableBar.updateFoo(foo -> foo.updateSomeOtherField(someOtherField -> ... ))

Currently updaters can be written by hand to the template class / interface if needed, but I see no reason why they could not be generated.

Also Optional (and Vavr Option etc.) could be taken account and offer an updater method which would transform the value only if it is present but this feature is not a must.

Chaining and doing this for multiple fields is of course inefficient but nothing prevents abusing withers in the same way.

@elucash
Copy link
Member

elucash commented Oct 12, 2022

Thank you for the feature request. The idea is good, maybe something like that was planned before, but we always tried to keep number of generated methods minimal. Ideally, we would have some sort of extensibility mechanism to add methods like these. The encodings (https://immutables.github.io/encoding.html) are a kind of extensibility I'm talking about, but these are per type, not applicable to any attribute. Indeed, this can be activated by a style flag in a form of naming pattern, empty by default - disabled (default "", can be "map*" -> giving mapFoo method for foo attribute. Need to think how much changes we need if implemented in a "good old way".

@ajkettun
Copy link
Author

That was fast! Thank you!

Hopefully records get withers some day and they creep up to regular immutable classes too:

https://github.com/openjdk/amber-docs/blob/master/eg-drafts/reconstruction-records-and-classes.md

@elucash
Copy link
Member

elucash commented Oct 13, 2022

yep, all good stuff comes to java only if you don't wait for it ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants