运算符
Slint 具有算术、比较、逻辑和三元运算符,以及成员访问和索引。 每个运算符都要求特定类型的操作数;除这里列出的少数几种外,Slint 不应用任何隐式强制转换, 操作数类型不匹配会构成编译错误。
优先级与结合性
Section titled “优先级与结合性”下表按从最松散到最紧密的绑定顺序列出所有运算符。 同一行上的运算符共享同一优先级。
| 运算符 | 描述 | 结合性 |
|---|---|---|
? : |
三元条件 | 右结合 |
|| && |
逻辑或、逻辑与 | 非结合 |
== != < > <= >= |
比较 | 非结合 |
+ - |
加法/拼接、减法 | 左结合 |
* / |
乘法、除法 | 左结合 |
+ - !(一元) |
正号、取负、逻辑非 | 前缀 |
. [] |
成员访问、索引 | 左结合 |
括号用于对嵌套表达式进行分组以覆盖优先级。
没有取模运算符;请改用 Math.mod。
数字后的 % 是单位(百分比),而不是运算符。
Slint 没有按位、移位、自增、自减或赋值运算符;绑定使用 :,
双向绑定使用 <=>。
比较运算符是非结合的:书写 a == b == c 或 a < b < c 是一个错误,
必须用括号括起操作数以消除歧义。
&& 和 || 彼此之间同样是非结合的:a && b || c 是一个错误,
必须将其中一侧用括号括起。
export component Example { in-out property <int> p: 1 * 2 + 3 * 4; // same as (1 * 2) + (3 * 4)}二元运算符 +、-、* 和 / 作用于数字(int、float、percent)以及
带单位的类型(length、physical-length、duration、angle、rem)。
+和-要求两个操作数为同一类型,或一个操作数为普通数字。 两个不同单位的值不能相加或相减。*将值与数字相乘,或将两个带单位的值相乘,得到一个单位乘积 (例如1px * 1px的类型为px²)。/将值除以数字或另一个值。 两个相同单位的值相除得到普通数字,因此self.width / 1px是作为float的宽度。
export component Example inherits Window { in property <length> a; in property <length> b; out property <length> total: a + b * 2;}export component Example { in-out property <length> w: 10px + 2px; // ok, same unit in-out property <length> h: 10px * 2; // ok, length times number in-out property <float> r: self.w / self.h; // ok, length over length}+ 运算符也可用于拼接字符串:如果任一操作数是 string,结果就是 string。
数字操作数(int 或 float)会转换为其字符串形式。
没有其他类型会被强制转换为 string,因此将 string 与例如 length 组合是一个错误;
请使用字符串模板来插入其他类型。
export component Example { out property <string> s: "count: " + 42; // "count: 42"}运算符 ==、!=、<、>、<= 和 >= 比较两个同一类型的操作数,
并产生 bool。 {#sls.op.comparison}
== 和 != 测试操作数是否相等。 {#sls.op.comparison.equality}
<、>、<= 和 >= 对数字、带单位的类型和字符串排序;对任何
其他类型的值排序是一个错误。
export component Example inherits Window { in property <int> a; in property <length> w; in property <length> h; out property <bool> equal: a == 0; out property <bool> different: a != 0; out property <bool> wider: w > h;}二元运算符 && 和 || 将两个布尔操作数组合为一个布尔值:
当两个操作数都为 true 时 && 为 true,|| 则在任一为 true 时为 true。 {#sls.op.logical}
一元运算符 ! 对布尔值取非。 {#sls.op.not}
操作数必须是 bool;其他任何类型都不会被强制转换为 bool。 {#sls.op.bool-operands}
export component Example inherits Window { in property <bool> a; in property <bool> b; out property <bool> both: a && b; out property <bool> either: a || b; out property <bool> neither: !a && !b;}&& 和 || 会短路:仅当左侧尚未确定结果时, 才会对右侧操作数求值。
前缀运算符为 +、- 和 !。 {#sls.op.prefix}
+ 和 - 作用于数字和带单位的类型,! 作用于 bool。
export component Example inherits Window { in property <length> w; in property <bool> flag; out property <length> negated: -w; out property <bool> toggled: !flag;}condition ? value1 : value2 对 condition(一个布尔值)求值,
当它为 true 时产生 value1,否则产生 value2。 {#sls.op.ternary}
两个分支具有相同的类型,该类型成为整个表达式的类型。 {#sls.op.ternary.type}
条件表达式是右结合的,
因此 a ? b : c ? d : e 分组为 a ? b : (c ? d : e)。 {#sls.op.ternary.assoc}
export component Example inherits Window { in property <bool> on; in property <color> active; in property <color> idle; out property <color> tint: on ? active : idle;}成员访问与索引
Section titled “成员访问与索引”. 按名字访问元素的属性、函数或回调,
以及结构体值的字段:
export component Example { foo := Rectangle { x: 42px; } x: foo.x;}方括号中的 [index] 通过从零开始的整数索引访问
数组的元素。
© 2026 SixtyFPS GmbH