Add a warning when taking the negative value of `.abs()` on a literal. (eg `-1.abs()`)
Bug Description
-1deg.abs() returns -1deg whereas Math.abs(-1deg) returns 1deg.
I expect -1deg.abs() equals 1deg.
Reproducible Code (if applicable)
export component AbsAngle inherits VerticalLayout {
Text {
text: -1deg.abs() / 1deg; // => -1
}
Text {
text: Math.abs(-1deg) / 1deg; // => 1
}
}
Environment Details
- Slint Version:
- Platform/OS: Linux/X11
- Programming Language: .slint
- Backend/Renderer:
Product Impact
No response
Is - applied to the result of 1deg.abs()?
Right, -1deg.abs() is the same as -(1deg.abs()).
This is the same precedence rules as in Rust, C++, and so on.
We can't change the precedence rules because then then things like -self.foo wouldn't work anymore because that would become (-self).foo
If anything we could detect this specific case and have a warning.
If anything we could detect this specific case and have a warning.
That might be an option, I agree. Otherwise I'd say that this isn't a bug.
We may improve the documentation for that.
https://github.com/slint-ui/slint/blob/ee6f78fc8df32fc1cb07585792eb703782f76f49/docs/astro/src/content/docs/reference/global-functions/math.mdx says
T type
Many of the math functions can be used with any <Link type="NumericTypes" label="numeric type"/> such asangle,duration,float,int,length, andpercent. These are represented on this page asT.General Math Functions
abs(T) -> T
Return the absolute value, where T is a numeric type.
Math.abs(-10); // returns 10 abs(-10); // returns 10 -10.abs(); // returns 10
This looks like I can write -10deg.abs().