slint-ui / slint

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

Add support for local variables #2752

Open tronical opened 1 year ago

tronical commented 1 year ago

Discussed in https://github.com/slint-ui/slint/discussions/2751

Originally posted by **loloof64** May 18, 2023 Hi, is it possible to define local variables in ui scripts components/functions ? I mean something like this for example ```javascript export component MySquare inherits Rectangle { width: 100px; height: 100px; background: red; in property items: "abcd/efgh/ijkl/mnop"; public function get_items_arr() -> [string] { let arr = []; for elt in items { if (elt != "/") arr.push(elt); } arr } } ```
ogoffart commented 1 year ago

Local variables might make some sense. But i would be hesitant to implement for loop. Our strategy had been to keep the language intentionally simple because the actual logic should be written in native code.

ogoffart commented 6 months ago

We would need to decide a syntax for it.

I'd go with let as this is the same syntax as rust and typescript.

let foo : string = "123";

(type annotation optional)

Tmpod commented 2 months ago

I agree with this. Having variables is useful sometimes, but Slint shouldn't really go deeper than that, and let the backend language handle that.

hunger commented 2 months ago

Maybe variable <string> foo: "bar"; ? That would align with the property syntax a bit more closely.

Or var <string> foo: "bar"; ?

FloVanGH commented 2 months ago

The code for the timer picker #5087 would also profit from local variables.