-
Notifications
You must be signed in to change notification settings - Fork 89
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
Feature request: noclobber
field attribute
#278
Comments
Can you provide more information about why you're trying to prevent "clobbering" of a builder value? Builders are nothing but bags of values that know how to turn themselves into the "target" type, and I'm struggling to imagine a scenario where swapping values in such a bag could do any actual harm. All RAII-type "work" or cross-field validation "work" should either be happening before passing a value to the builder, in which case |
The underlying idea is to use the builder as a kind of typestate machine, so that a field value, once set, is known to be definitely expressed in the final struct. It's possible to do this work ahead of time, but it's also possible to do this work within the type system, and the latter can be preferable sometimes. In other words, yes, I can (and have, for the immediate case) written ahead-of-time validation which ensures that we don't attempt to redundantly set particular values. However, it would have been even easier for me if I didn't have to write that, and could instead write |
Typesafe builders were initially discussed in #33, and the conclusion since then has been that they're interesting, but out of scope for
Since typesafe builders are out of scope, that leaves only the alternative of a
Existing Support/Workarounds
Optional set-once fields are possible two ways: Either by adding custom methods to the builder in an Finally, if the desire to avoid clobbering comes from the builder's caller instead of the builder itself, using ConclusionGiven the problems described above and the existence of workarounds, I would prefer not to address this within Thank you for submitting this idea, and taking the time to provide a great writeup of the concept, the proposed experience, and the reasoning behind it. While this particular idea isn't a fit for |
It is sometimes useful to ensure that a builder value, once set, is not allowed to be reset before calling
build
. For example:Instead, I'd like something like:
There are at least two different patterns which could potentially be used to prevent duplicate sets: typestates and fallible results.
The typestate method is in my opinion somewhat more elegant, though it makes autogenerated documentation for the builder somewhat worse. In this method, the builder gains a generic attribute for every field, and the build method is only implemented for the appropriate typestate:
Fallible setters seem more likely to compose well with other existing
derive-builder
features. In that case, what we end up with isI have no strong opinions about which pattern gets used, but one way or the other, support for
noclobber
would be useful to me.The text was updated successfully, but these errors were encountered: