Builder
See original GitHub issueIs your feature request related to a problem? Please describe. When creating objects which have multiple properties and constraints, like required values, the builder pattern is helpful so we don’t need a constructor with a lot of parameters.
Describe the solution you’d like
The [Builder]
attribute should generate a static method Builder()
which returns an instance of a generate class SomeClassBuilder
which has one method per property (eg, .Name("my name")
) and a method build()
that creates that runs all the checks and creates the final class calling its constructor. See the lombok documentation for details.
Describe alternatives you’ve considered For now, an alternative is to generate the all args constructor.
Additional context A simple implementation could be shipped without the need of all the caveats and features that the lombok implementation has.
Issue Analytics
- State:
- Created 3 months ago
- Comments:11 (6 by maintainers)
Top GitHub Comments
I agree with the syntax. However throwing an exception when a user does not call a specific method seems kind of ugly. That should be up to the user to validate. This package deals with compile-time assistance. I don’t feel comfortable generating code which errors at runtime.
I have thought about this some more and am not happy with any of the possible solutions I have come up with, which is why I will close this for now and see if I can implement a much simpler form of the builder pattern in the future. I fear that the generated code will need to be made configurable and that will be a lot of work.