Skip to content

Add support for local variables #2752

Open
@tronical

Description

@tronical

We should support local variable to be able to cache intermediate computation result in a binding.

So we could do things like

function foobar() -> int {
    let value = compute-value();
    return value != 0 : root.property / value ? root.some-other-value;       
}

In other word, add support for

let something = <expression>;

in the Slint language.

Discussed in #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

export component MySquare inherits Rectangle {
    width: 100px;
    height: 100px;
    background: red;
    in property<string> items: "abcd/efgh/ijkl/mnop";

    public function get_items_arr() -> [string] {
        let arr = [];
        for elt in items {
            if (elt != "/") arr.push(elt);
        }
        arr
    }
}
```</div>

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:language-slintCompiler for the .slint language (mO,bF)good first issueGood for newcomersrfcRequest for comments: proposals for changes

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions