-
-
Notifications
You must be signed in to change notification settings - Fork 20
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
Supply required fields as args to builder()
#24
Comments
I didn't think of having a macro to derive the |
Yeah, I agree there have been solutions but I think bon could do it much better - for example, #[bon::new]
struct ExampleStruct {
#[bon(from-iter)]
field: HashSet<u32>,
optional: Option<usize>,
}
fn main() {
let example_struct = Example::new([0, 1, 2]).optional(3);
} I would definitely prefer to use a feature like this built into bon, as then it could use the same 'ecosystem' of features such as |
Note that this has the problem of allowing you to set the value for the same optional field more than once: Example::new([0, 1, 2]).optional(3).optional(4); This can happen if two optional fields have the same type and are named almost the same e.g. |
Yeah I agree that's a potential drawback, but lots of Rust APIs use this pattern with a 'new' method where you supply required methods, then optional methods you can chain which set or update other fields, and I think it works quite well. Depending on the use case, I think this pattern works better than the builder pattern currently possible with bon, because it's less verbose, not requiring you to name required fields - e.g when you only have one required field it removes the need to call |
You could also add the option to supply setters which take in |
I think it's reasonable. Eventually, we'll get there |
Yeah, I definitely understand this is non-trivial to implement and so will be low on the priority list for now! |
I think this is implemented now!? |
Yes, according to the title, the issue was implemented in #125. While discussing it we determined that the author of the issue had a bit different requirements in mind. I created separate issues for them: |
I'd love to have the option to supply required fields of a struct as args to the first method call (basically deriving a
new
method), leaving optional ones as separate methods you would then be able to call. This would also mean you should't have to call.build()
at the end of the chain, because the data structure is valid 'from the get-go'. This would be great, as currently I usederive-new
for when I have a simple struct and can't be bothered to write thenew
function, but having this built into bon would be much nicer as setters for optional methods would be derived too.A note for the community from the maintainers
Please vote on this issue by adding a 👍 reaction to help the maintainers with prioritizing it. You may add a comment describing your real use case related to this issue for us to better understand the problem domain.
The text was updated successfully, but these errors were encountered: