-
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
Getters #7
Comments
It would be great to have a solution that allowed the user to use the builder as something like a rust-typemap. (Yes I know it is out of scope, but it would be great to have.). I tried to propose something similar over here |
@nielsle thx for your suggestion. But I don't see that in this crate, because it's a different use case with different requirements. But feel free to create a fork of derive_builder and replace https://github.com/colin-kiegel/rust-derive-builder/blob/master/src/derive_builder.rs#L47 and https://github.com/colin-kiegel/rust-derive-builder/blob/master/src/derive_builder.rs#L80 |
as long as we can't make this idiomatic, I would rate a workaround with low priority. It is already possible to make the fields public, which seems good enough as a workaround. |
If a field is unset but has a default value, should the getter return Note: Default values can be arbitrary functions with side-effects. |
@colin-kiegel I was thinking about that as part of the work on #85. As shown in the sample code in #79, I'd like to expose at least private getters with the signature The downside seems to be that they require doing one of the following:
|
What about this use case, where someone wants to query the builder whether a field is already set? If we don't include computing the default value, then we could allow this via If we decide to include computing custom defaults, we could also
|
The "is set" case can be checked by querying the field directly, especially if the author is writing a custom |
Ok, I was thinking about the three-crate-scenario (i) derive_builder, (ii) author of builder, (iii) user of builder. I'm wondering if there could be a use case where the user of the builder wants to inspect the input already given to the builder. You're saying this would only be possible if the author of the builder already made the fields public (which is probably an un-idiomatic choice as discussed in #86). Maybe such a use case is unlikely. I can only imagine a cases where the user of the builder uses dynamic input and either (a) wants to conditionally apply a default value which is different from the stock default, but the setters have once-only-semantics or (b) wants to display the given inputs somehow, like "please review your |
Another possible problem I can imagine is this. I would very much like Getters would have to return And is it really safe to say that users of the builder should not depend on getters to fail in a specific situation (like no value was supplied)? Because otherwise adding |
This issue hasn't had any movement in ~5 years, so I'm concluding that this doesn't come up much in practice and am closing the issue. |
Thanks. Well, Arti is the first project I've done significant work on that uses builder patterns for its configuration, but I understand the builder pattern is quite common. I don't think anything we're doing in Arti is particularly unique. Rust is still quite young in some of these areas, so I don't find it surprising that I'm breaking new ground. Within Arti: I looked at my outstanding work and my tree has 24 uses of
I don't think they would be right, because the accessors need to take
That's true. But, I have seem soo many bugs where even adjacent repetition has been botched. This is still very true in Rust (where everyone runs rustfmt, which sadly can't maintain tabuler layout). If we end up writing code like this, the bug is too easy to write and too hard to spot:
I believe that would be possible, but I don't think it is a very good design. It seems like an antipattern to set out to make a proc macro whose purpose is to massage the input for a diffeerent proc macro. The layering of abstractions becomes quite confusing, and the "wrapper" macro now has to unparse the inner proc-macro's input. I think this is a thing one would only do if there were compelling reasons why the "inner" macro couldn't be modified to add the desired features. (It's a different matter if the two proc macros are semantically orthogonal: I often combine proc macros and am sometimes disappointed when the interact when I wish they wouldn't...) |
I wrote in #4 (comment):
The text was updated successfully, but these errors were encountered: