slint-ui / slint

Slint is a declarative GUI toolkit to build native user interfaces for Rust, C++, or JavaScript apps.
https://slint.dev
Other
16.93k stars 568 forks source link

Adding `Math.sign()` #6205

Open Enyium opened 3 days ago

Enyium commented 3 days ago

Please add Math.sign(), which returns -1, 0, or 1, and should accept various data types. It is also available in browsers. I needed it with a custom component wrapping SpinBox and changing its behavior.

ogoffart commented 3 days ago

It's true that JS has that so we might as well add it. Rust has signum

It is easy to add it to your project

global MyMath {
   public pure function sign(v: float) -> float { 
        return v < 0 ? -1 : v > 0 ? 1 : 0;
   } 
}

(If this was to be implemented in Slint, it could be done as a builtin macro that would be lowered in builtin_macro.rs to the equivalent of the function above)