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
Field-level #[builder(default = __)] easier to use wrong than right #65
Comments
Hm, thanks. There was a discussion about this here (comparison with serde / question 1) and in this comment below:
The outcome of that discussion was, please delegate to methods if that makes sense (complex stuff), or use arbitrary expressions if you know what you are doing (only really recommended for simple const expressions). The docs clearly recommends delegating to helper methods. I still believe that 100% serde-behaviour is not a good fit for derive_builder. E.g. in serde you cannot write |
This is accurate. Is there maybe a way to check at compile time that the user has either passed:
That could disallow the exotic misuses while still allowing the desired simple case. |
A custom derive can only inspect the source code of the item it is applied to. Nothing more - so we don't know what's in scope and what implements what. We're completely blind to the world. :-) |
:( bummer. Okay, in that case I'm out of ideas on this one. |
I thinks it's the best we can do right now. I'll close in the meantime. |
This produces the following error:
Alternatively, some really weird things can be accomplished:
The only time the current implementation of this will work at the field level is if the user provides a literal or a value that implements
Copy
. Serde avoids this by requiring the value ofdefault
to be a function with the signatureFn() -> T
, which it calls every time it needs a new instance. That feels much more hygienic than the current behavior (and makes usingdefault
in conjunction with helper or factory functions much cleaner).@colin-kiegel, how would you feel about combining this with #61 and documenting the breaking change as "aligned
default
behavior with serde"?The text was updated successfully, but these errors were encountered: