Is there a way to get a builder for a struct ? #99
-
For example :
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
This crate does not support it, and I don't think it should. Typed Builder's first and foremost purpose is to allow fields with default values to get their default values while ensuring, at compile time, that all the mandatory fields are set. If you create a builder from a struct, all the fields are already set - making this enforcement redundant. Typed Builder also verifies that no field is set in the builder more than once. This is actually a problem in your case - if the builder is initialized by the original struct, then any field you set in it will be set a second time. Of course, this can be resolved by deciding that fields can be set once in the builder in addition to being initialized from the struct, but because of the architecture of Typed Builder where being able to store a value is the very indicator of whether or not it was set, this is going to be very tricky to implement. Also, one of the reasons for this crate was a limitation of the let new_foo = Foo {
height: 1,
..foo
}; |
Beta Was this translation helpful? Give feedback.
-
Thanks. I appreciate your detailed explanation and totally agree with you. And thanks for this great library |
Beta Was this translation helpful? Give feedback.
This crate does not support it, and I don't think it should.
Typed Builder's first and foremost purpose is to allow fields with default values to get their default values while ensuring, at compile time, that all the mandatory fields are set. If you create a builder from a struct, all the fields are already set - making this enforcement redundant.
Typed Builder also verifies that no field is set in the builder more than once. This is actually a problem in your case - if the builder is initialized by the original struct, then any field you set in it will be set a second time. Of course, this can be resolved by deciding that fields can be set once in the builder in addition to being initial…