You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.
To add a child to a parent builder, it needs something like this: Parent.builder().child(Child.builder().nickname("Freddy").build()).build();
What might be interesting is to allow something like this: Parent.builder().childBuilder().nickname("Freddy").build().build();
I appreciate it's a pretty minor reduction in boilerplate, but think it adds an option that might look nicer, and think it could be done with the following idea:
Have 2 constructors in each builder, a default one and another one which takes a function which takes a single parameter of the built object and returns a generic value. Then in the build() function if the function wasn't given proceed as usual, and if it was given, build the object as normal, but then invoke the given function with the built object and return the result.
This could then be used by a parent builder to add/set the child object as normal then return itself behind the scenes to continue building the parent.
The text was updated successfully, but these errors were encountered:
This is just a smidge shorter than your suggestion, and doesn't require a whole separate Child.Builder class (which unfortunately yours would as the build method has a different signature).
Hi, awesome library!
I have some classes I want to add builders for that have a nested structure, here's a noddy example:
To add a child to a parent builder, it needs something like this:
Parent.builder().child(Child.builder().nickname("Freddy").build()).build();
What might be interesting is to allow something like this:
Parent.builder().childBuilder().nickname("Freddy").build().build();
I appreciate it's a pretty minor reduction in boilerplate, but think it adds an option that might look nicer, and think it could be done with the following idea:
Have 2 constructors in each builder, a default one and another one which takes a function which takes a single parameter of the built object and returns a generic value. Then in the
build()
function if the function wasn't given proceed as usual, and if it was given, build the object as normal, but then invoke the given function with the built object and return the result.This could then be used by a parent builder to add/set the child object as normal then return itself behind the scenes to continue building the parent.
The text was updated successfully, but these errors were encountered: