Functions
Similar to other programming languages, functions in Slint are way to name, organize and reuse a piece of logic/code.
Functions can be defined as part of a component, or as part of an element within a component. It is not possible to declare global (top-level) functions, or to declare them as part of a struct or enum. It is also not possible to nest functions within other functions.
Declaring Functions
Functions in Slint are declared using the function
keyword. For example:
Functions can have parameters which are declared within parentheses, following the format name: type
.
These parameters can be referenced by their names within the function body. Parameters are passed by
value.
Functions can also return a value. The return type is specified after ->
in the function signature.
The return
keyword is used within the function body to return an expression of the declared type.
If a function does not explicitly return a value, the value of the last statement is returned by default.
Functions can be annotated with the pure
keyword.
This indicates that the function does not cause any side effects.
More details can be found in the Purity chapter.
Calling Functions
A function can be called without an element name (like a function call in other languages) or with an element name (like a method call in other languages):
Function Visibility
By default, functions are private and cannot be accessed from other components.
However, their accessibility can be modified using the public
or protected
keywords.
- A root-level function annotated with
public
can be accessed by any component.
To access such a function from a different component, you always need a target, which in practice means the calling component must declare the called component as one of its child elements.
If a function is declared in a child element, even if marked public, it is not possible to call it from another component, as the child elements themselves are not public and a valid target for the function does not exist:
Functions marked public
in an exported component can also be invoked from backend code (Rust, C++, JS).
See the language-specific documentation for the generated code to use.
- A function annotated with
protected
can only be accessed by components that directly inherit from it.
Functions vs. Callbacks
There are a lot of similarities between functions and callbacks:
- They are both callable blocks of logic/code
- They are invoked in the same way
- They can both have parameters and return values
- They can both be declared
pure
But there are also differences:
- The code/logic in the callback can be set in the backend code and implemented in the backend language (Rust, C++, JS), while functions must be defined entirely in slint
- The syntax for defining a callback is different
- Callbacks can be declared without assigning a block of code to them
- Callbacks have a special syntax for declaring aliases using the two-way binding operator
<=>
- Callback visibility is always similar to
public
functions
In general, the biggest reason to use callbacks is to be able to handle them from the backend code. Use a function if that is not needed.
Callbacks
Components may declare callbacks, that communicate changes of state to the outside. Callbacks are invoked by “calling” them like you would call a function.
You react to callback invocation by declaring a handler using the =>
arrow syntax.
The built-in TouchArea
element declares a clicked
callback, that’s invoked
when the user touches the rectangular area covered by the element, or clicks into
it with the mouse. In the example below, the invocation of that callback is forwarded
to another custom callback (hello
) by declaring a handler and invoking our
custom callback:
It’s possible to add parameters to a callback:
Callbacks may also return a value:
Callback arguments can also have names. The names of arguments have currently no semantic value, but they improve readability of your code.
Aliases
It’s possible to declare callback aliases in a similar way to two-way bindings:
© 2024 SixtyFPS GmbH