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
[suggestion] include build
method when all required fields are not available.
#2
Comments
Yes if I'm not mistaken, the number of generated Suppose we have Another problem is the functions inside an impl Builder<false, false> {
fn build(&self, req1: usize, req2: usize) -> Builder<true, true> { ... }
fn req1(&self, req1: usize) -> Builder<true, false> { ... }
fn req2(&self, req2: usize) -> Builder<false, true> { ... }
} You can imagine if we had more required parameters, we had to connect all the I agree that if the user forgets to initialize a required parameter, the generated error by the compiler is cryptic and unhelpful. I think the problem you're trying to solve here is really a UI problem that I don't have a solution for unfortunately. |
Yes, that's its biggest issue. I've tried a few other tricks, this one worked pretty well but has its own issues: trait IncompleteBuilder {
/// SAFETY:
/// missing fields
unsafe fn build() -> ! { panic!("build() was called when the builder is incomplete") }
}
impl<const A: bool, const B: bool, const F: bool> IncompleteBuilder for ItemBuilder<A, B, F> {
} which works fine (if we define it after |
Doesn't this design kind of defeats the purpose of a compile-time correct builder? I may be completely missing the point of your proposed design. So please bear with me until we figure it out :) |
I think you've missed the |
After testing it in the playground, now I get what you meant. It works but if I'm not mistaken, as I tested it in the playground, calling the |
Unfortunately, the compiler doesn't give us many tools to show custom error messages to the user. the closest thing I've found is the combination of the |
https://gist.github.com/sahandevs/00ac326829c83926e78fd91e3a37e883 I'll send a PR and put the above implementation behind a nightly only feature gate if you are ok with it. |
Neat solution :) |
In order for users to know which fields are missing we can do something like this:
The only problem is that it generates a lot of code and it may not be ideal. But if you are OK with the design, I can send a PR if you want to.
The text was updated successfully, but these errors were encountered: