赋值
给变量赋值只有一种方式:$x = 3。$ 是变量名的一部分,无论出现在哪里,都按写法来设置。第一次赋值会在当前代码块中声明 $x;之后,同一语句(包括在嵌套代码块或函数内部)会修改它。其他运算符用于更新已存在的变量:$total += 1、$name ??= 'guest'、$point.x = 3、$list[0] = 'a'。
<let name="total" value="0" />
<for each="item" in="$cart">
<set name="total" op="+=" value="$item.price" /> <!-- changes the total above -->
<let name="line" value="$item.name" /> <!-- new: only exists inside the loop -->
</for>
<const name="tax" value="0.2" /> <!-- can't be changed -->
<let name="count" type="int" value="$raw" /> <!-- declared with a type (converted, or an error) -->$total = 0
for item in $cart {
$total += $item.price // changes the total above
$line = $item.name // new: only exists inside the loop
}
const $tax = 0.2 // can't be changed
$count: int = $raw // declared with a type (converted, or an error)let 和 set 不用于 .kscript 文件;在这些文件中写入它们会报错,并提示去掉该词。在 XML 中它们保持原有含义(<let> 在当前作用域声明,<set> 用于修改),kscript convert 会选择正确的写法。