-
Notifications
You must be signed in to change notification settings - Fork 50
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
Auto-generate a Step Builder? #33
Comments
Sort of trying to achieve named-parameters for data constructors. |
I would say that using newtype is a better solution to avoid mixing parameters, but it is true that we won't have cheap newtype until jdk10... on the other hand the builder pattern is also a bit heavy... |
Some purist will be unhappy with this comment. But maybe combining the popsicle pattern with the step builder pattern to insure 1 memory allocation per object. E.g.
Produce:
And as long as the end-user can not get a hold of the Maybe something like the following can work:
And the user can still do it the other way too.
|
@clinuxrulz The purest in me says "oh hai, you want curried constructors?" of some description. public static Function<Double, Shape> rectangle(double width) {
return height -> Shapes.rectangle(width, height);
}
public static Function<Double, Function<Double, Shape>> cube(double width) {
return height -> depth -> Shapes.rectangle(width, height, depth);
} and called with: Shape rect = Shapes.rectangle(100.0).apply(100.0);
Shape cube = Shapes.cube(100.0).apply(100.0).apply(100); With any more parameters, that return declaration would get mighty nested, but I guess you could also generate something like: public static class CubeBuilder extends Function<Double, Function<Double, Shape>> {
}
public static CubeBuilder cube(double width) {
return height -> depth -> Shapes.rectangle(width, height, depth);
} which just reshapes things. |
@talios That looks ideal. However does javac optimize curried lambda application? My use cases involve over 30 arguments to a type constructor, which would equate to 31 memory allocations per object (if javac does not optimize curry) Maybe its just premature optimization on my part. Edit: |
On a side note. FunctionalJava's
|
@clinuxrulz AFAIK the first immediately returned lambda will be compiled as a static/singleton since it doesn't close over anything, but since the subsequent instances close over the earlier result, each invocation will create a new instance of a 30 arguments.... ug - yeh, you probably wouldn't want to curry that - that would be 30 function instantiations and invocations and gcs. I wouldn't exactly want to do that in a tight loop :) |
One advantage of builder pattern is descriptive method names, like so: Shape cube = Shapes.cube().width(100.0).height(80.0).depth(90.0); Easier to read than if |
For now zerobuilder is solution. |
@jbgi I'm thinking of exposing zerobuilder's core functionality as a library (more or less what Generator.generate does, see http://bit.ly/2dSjvav) |
@h908714124 yeah that would be great! |
I hope to release a separate artifact |
Api is already released. See https://github.com/h908714124/zerobuilder/tree/master/api |
Can be handy if a data constructor has a lot of fields, and you want to insure the user of your data structure supplies the fields in the correct order. (avoid mixing up fields with the same type, like
width
andheight
)The text was updated successfully, but these errors were encountered: