Negative literals don't work
Problem
error: the property value must be either a literal or enclosed in braces. Consider adding braces around your expression.
--> src/components/slider.3.example:13:25
|
13 | <Slider min=-100f64 max=100f64 {onchange}/>
| ^^^^^^^
Using a positive literal works.
Steps To Reproduce Steps to reproduce the behavior:
- See example above
Expected behavior
Negative literals should work the same way as positives do.
Screenshots
See above.
Environment:
- Yew version:
0.19 - Rust version:
1.59 - Target, if relevant:
wasm32-unknown-unknown - Build tool, if relevant:
trunk
Questionnaire
- [x] I'm interested in fixing this myself but don't know where to start
- [ ] I would like to fix and I have a solution
- [ ] I don't have time to fix this right now, but maybe later
Negative literals are not literals but expressions, so the (pedantic) error is correct to the letter. But I agree that we could allow this case in the macro parser. I wouldn't want to go far beyond negative literals though. Switching to general expressions would only introduce more edge cases, but "negative literals" seem to be fine for user-convenience.
Negative literals are not literals but expressions …
Thanks for sharing this! That explains the issue.
Yes, it would be nice to have this a bit more convenient. But indeed, I would expand this to full expressions.
The requirement for braces has been introduced as part of #1939. There you should find reasons for requiring braces in most cases. One of those would be that it allows yew to introduce other syntax and prop-shorthands going forward without breaking existing code.
I think it would be reasonable to (re-)include the following pieces of syntax without having to put braces around:
- negated literals
- expressions surrounded with
()parenthesis, including tuple expressions - array expressions + repeat expressions, although they I don't immediately see which prop type would bind such an expression
- unsafe, try and async blocks. probably fringe cases, not sure if it's useful to cater to these
I don't think we should expand upon literals anymore. By adding exceptions, we create the possibility of running into edge cases. There's also the argument of, "if negative literals are fine, why not X." I don't think that prop=-2 is any better than prop={-2}. The latter seems cleaner to me
Is this something we want to support? I would like to only allow what Rust considers as literals. Negative numbers are not that, so they should require to be wrapped in { }.
I think this issue can be closed since no change is required here.
cc: @yewstack/yew
I'd leave it open in case we get around to a documentation PR clarifying this issue. I don't think we should support this, but the idea that "negative numbers are not literals, but positive ones are" is not an intuitive one.