-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add default
value for properties (takes a function instead of an expression)
#881
Conversation
I think at the very least you should look at prior art in how Serde accomplishes this. If they can't get a correct error message in this situation, I don't think it would be reasonable to expect you to do the same - and I don't think it would be a deal breaker for the feature, although the documentation where this feature is mentioned should make light of the erroneous error message. |
@hgzimmerman Thanks for pointing that out. I checked serde's source code and did some experiment. The error message serde would provide in such case is Inspired by serde, I did something like: let none: Option<#ty> = None;
match true {
false => none,
true => Some(#default())
}
.unwrap() Now the error message becomes:
Still confusing, but at least it won't suggest a wrong fix. There might be better ways to fix this. |
// Hacks to avoid misleading error message. | ||
quote_spanned! {span=> | ||
#name: { | ||
let none: Option<#ty> = None; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the Option
is a bit confusing. How about the following:
#name: {
match true {
false => ::std::unimplemented!(),
true => #default()
}
},
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated suggestion to use ::std::
prefix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, great test coverage 👍
// Hacks to avoid misleading error message. | ||
quote_spanned! {span=> | ||
#name: { | ||
let none: Option<#ty> = None; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated suggestion to use ::std::
prefix
#[allow(unreachable_code)] | ||
false => { | ||
let __unreachable: #ty = ::std::unreachable!(); | ||
__unreachable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you need an assign here? I think you can just do false => ::std::unreachable!()
right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The false
arm must return the correct type, otherwise Rust would think that it has the same type as #default()
, and might show that misleading message: "help: try using a conversion method: "foo".to_string()
".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm your explanation makes sense but I had it working with unimplemented like that. This is fine though!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I was wrong about that. I did not test it before leaving the comment. It seems that false => ::std::unreachable!()
does work.
…pression) (yewstack#881) * Add `default` value for properties * Default values specified by paths instead of exprs * Fix misleading error message * Remove confusing Option
Fix #775
Similar to #879 , but default values are specified by paths to functions, instead of expressions:
But there is a minor problem:Fixed.If the function has incompatible type:
It might produce an error message with a misleading "help":
I don't know how to fix this.
The expression version (#879 ) has a similar problem.
@mdtusz @jstarry @hgzimmerman