表达式
+ - * / % **、== !=(宽松:'3' == 3)、=== !==(严格)、< <= > >=、and or not、??、?.、a ? b : c、in、列表 [1, 2] 与映射 { name: 'Joan' } 字面量(列表会用 as set 转为集合)、$p.name、$list[0](负索引从末尾开始计数)、范围 0..10 与 0..100 step 5(不含结束值,也可带步长如 0.1)以及 1..=10(含结束值)、match $x { 1 => 'one', _ => 'other' }、->method()、|> 和 fn(x) => …。使用 and 和 or 组合条件。前缀 ! 与 not 具有相同的优先级和真值判断,因此 !$ready、! $ready 和 not $ready 等价。可用于字段(!$user.active)、整个条件(if !($ready and $open) { … })或插值("{ ! null->is_null() }")。写法 null->!is_null() 在链中下一个环节之前对方法结果取反。XML 同样为每种比较提供了单词形式(lt le gt ge eq ne),供带引号的属性内使用,因为 XML 在那里不能直接包含原始的 <;简写起始标签接受原样写的 < 和 >。在 .kscript 文件中,这些单词会被报告(KS6012),kscript check --fix 则会写成符号。
用 <、<=、> 或 >= 比较数字与字符串时,如果该字符串可以解析为数字,KScript 会按数值比较。若解析失败,比较结果为 false。比较可以像数学中那样连锁,数学一节对此有所说明。使用 == 判断两个日期是否相等时,只有当它们是同一时刻(包括秒)才相等。
数学
日常数学运算有其自己的符号,每个符号在脚本、模板和数据库查询中的作用都相同。+ 为加法,- 为减法,* 为乘法,/ 为除法,保留小数部分。~/ 为整除,向下取整。% 给出余数,余数的符号与左侧数字一致,%% 像时钟一样循环取模,** 为乘方:
<let name="minutes" value="135" />
<let name="hours" value="$minutes / 60" /> <!-- 2.25 because / keeps the fraction -->
<let name="whole" value="$minutes ~/ 60" /> <!-- 2 because 60 fits into 135 twice -->
<let name="left" value="$minutes % 60" /> <!-- 15 because 135 - 2 * 60 leaves 15 -->
<let name="behind" value="-7 % 3" /> <!-- -1 because % keeps the sign of -7 -->
<let name="area" value="4 ** 2" /> <!-- 16 because 4 * 4 is 16 -->
<let name="kilobyte" value="2 ** 10" /> <!-- 1024 because ten 2s are multiplied -->$minutes = 135
$hours = $minutes / 60 // 2.25 because / keeps the fraction
$whole = $minutes ~/ 60 // 2 because 60 fits into 135 twice
$left = $minutes % 60 // 15 because 135 - 2 * 60 leaves 15
$behind = -7 % 3 // -1 because % keeps the sign of -7
$area = 4 ** 2 // 16 because 4 * 4 is 16
$kilobyte = 2 ** 10 // 1024 because ten 2s are multiplied运算符按以下顺序结合,从最紧密到最松散,因此 2 + 3 * 4 的结果是 14,因为 * 先于 + 结合:
| 优先级 | 运算符 | 分组 |
|---|---|---|
| 1 | . -> ?. ( ) [ ] |
从左到右 |
| 2 | ** |
从右到左:2 ** 3 ** 2 即 2 ** 9,512 |
| 3 | -x +x |
-2 ** 2 结果为 -4,因为 ** 先结合,因此应写作 -(2 ** 2) 或 (-2) ** 2(KS6023) |
| 4 | * / ~/ % %% |
从左到右 |
| 5 | + - |
从左到右 |
| 6 | .. ..= step |
每个表达式只能有一个范围 |
| 7 | |> |
从左到右 |
| 8 | < <= > >= in |
单向链:0 <= $n < 10 |
| 9 | == != === !== |
每个比较一个;要连接更多请使用 and |
| 10 | not, and, or, ??, a ? b : c |
% 与 %% 只有在符号为负时才有区别。余数的符号与左侧一致,与 C# 和 JavaScript 相同。取模的符号与右侧一致,因此会循环:午夜前一小时是 23,而不是 -1。对于时钟、星期以及在列表末尾循环的位置,请使用 %%:
<let name="hour" value="9 - 12" /> <!-- -3 because 3 hours before 9 -->
<let name="late" value="$hour % 24" /> <!-- -3 because % keeps the sign of -3 -->
<let name="clock" value="$hour %% 24" /> <!-- 21 because %% wraps back round the clock -->
<let name="days" value="['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']" />
<let name="today" value="0" /> <!-- 'mon' is at 0 -->
<let name="yesterday" value="$days[($today - 1) %% 7]" /> <!-- 'sun' because -1 wraps round to the last day -->
<let name="spare" value="-15m %% 1h" /> <!-- 45m because durations wrap the same way -->$hour = 9 - 12 // -3 because 3 hours before 9
$late = $hour % 24 // -3 because % keeps the sign of -3
$clock = $hour %% 24 // 21 because %% wraps back round the clock
$days = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']
$today = 0 // 'mon' is at 0
$yesterday = $days[($today - 1) %% 7] // 'sun' because -1 wraps round to the last day
$spare = -15m %% 1h // 45m because durations wrap the same way~/ 向下取整,而不是向零取整,因此它与 %% 配对使用:($a ~/ $b) * $b + $a %% $b 始终等于 $a。它也适用于时长和金额,并返回普通数字:
<let name="balance" value="-7" />
<let name="weeks" value="$balance ~/ 2" /> <!-- -4 because it rounds down, not toward zero -->
<let name="rest" value="$balance %% 2" /> <!-- 1 because -4 * 2 + 1 is -7 -->
<let name="runtime" value="2h10m" />
<let name="episodes" value="$runtime ~/ 25m" /> <!-- 5 because five 25m episodes fit into 2h10m -->
<let name="spare" value="$runtime %% 25m" /> <!-- 5m because 2h10m - 5 * 25m leaves 5m -->$balance = -7
$weeks = $balance ~/ 2 // -4 because it rounds down, not toward zero
$rest = $balance %% 2 // 1 because -4 * 2 + 1 is -7
$runtime = 2h10m
$episodes = $runtime ~/ 25m // 5 because five 25m episodes fit into 2h10m
$spare = $runtime %% 25m // 5m because 2h10m - 5 * 25m leaves 5m// 开始一段注释,因此它不能用于整数除法。$hours = $minutes // $per_hour 不会改动 $minutes,而 KS6022 会指出这一点,并给出一个写成 ~/ 的修复建议。
方向相同的比较可以像数学中那样连写,因此范围检查读起来与口头表述一致。中间的每个值只计算一次,链条在第一个为假的比较处停止:
<let name="score" value="42" />
<let name="passed" value="0 <= $score < 100" /> <!-- true because 42 is from 0 up to (not including) 100 -->
<let name="top" value="90 <= $score <= 100" /> <!-- false because 42 is below 90 -->
<let name="falling" value="10 > 7 >= 7 > 1" /> <!-- true because each step goes down or stays the same -->$score = 42
$passed = 0 <= $score < 100 // true because 42 is from 0 up to (not including) 100
$top = 90 <= $score <= 100 // false because 42 is below 90
$falling = 10 > 7 >= 7 > 1 // true because each step goes down or stays the same连写的比较必须朝同一方向。1 < $n > 0 是错误的,因为它没有说明 1 与 0 的大小关系,应写成 1 < $n and $n > 0。== 和 != 也不能连写:$a == $b == $c 会与 $c 比较出真或假,应写成 $a == $b and $b == $c。括号会中断连写,因此 (1 < $n) == true 比较的是结果。连写在数据库查询中同样适用:Person->where(fn(person) => 18 <= $person.age < 65) 表示 age >= 18 AND age < 65。->between() 已被移除,其错误信息会指出应改用的连写形式。
.. 生成的范围不包含终点,适合从 0 开始计数以及表示列表中的位置。..= 则包含终点,适合从 1 开始计数。将鼠标悬停在任一项上,可以看到范围的起点和终点:
<let name="positions" value="0..3" /> <!-- [0, 1, 2] because .. stops before 3 -->
<let name="places" value="1..=3" /> <!-- [1, 2, 3] because ..= takes in 3 -->
<let name="quarters" value="0..=1 step 0.25" /> <!-- [0, 0.25, 0.5, 0.75, 1] because a step lands on 1 -->
<let name="odd" value="1..=10 step 2" /> <!-- [1, 3, 5, 7, 9] because no step lands on 10 -->
<let name="monday" value="'2026-11-30'->to_date()" />
<let name="week" value="$monday..=$monday + 6d" /> <!-- seven dates because ..= takes in the Sunday -->$positions = 0..3 // [0, 1, 2] because .. stops before 3
$places = 1..=3 // [1, 2, 3] because ..= takes in 3
$quarters = 0..=1 step 0.25 // [0, 0.25, 0.5, 0.75, 1] because a step lands on 1
$odd = 1..=10 step 2 // [1, 3, 5, 7, 9] because no step lands on 10
$monday = '2026-11-30'->to_date()
$week = $monday..=$monday + 6d // seven dates because ..= takes in the Sunday与 .. 类似,当范围的终点小于起点时,范围会递减,因此 3..=1 等同于 [3, 2, 1],1..=0 等同于 [1, 0]。若要向上计数到一个可能为 0 的值(如 1..($count + 1)),请保留 ..,当 $count 为 0 时它不会产生任何值。
^ 在 KScript 中不是幂运算(在 C# 和 JavaScript 中它是按位运算符),因此 2 ^ 10 是一个错误,其修复建议会写成 2 ** 10。
位运算通过一个方法 ->bits() 完成,而不是使用符号,因为 & 和 | 很容易被误认为 and 和 or。该方法逐步作用于整数:
<let name="read" value="4" />
<let name="write" value="2" />
<let name="permissions" value="6" /> <!-- read and write, because 4 + 2 is 6 -->
<let name="can_read" value="$permissions->bits(and: $read) == $read" /> <!-- true because 6 has the 4 bit -->
<let name="with_run" value="$permissions->bits(or: 1)" /> <!-- 7 because or: adds the 1 bit -->
<let name="toggled" value="$permissions->bits(xor: $write)" /> <!-- 4 because xor: flips the 2 bit off -->
<let name="doubled" value="$permissions->bits(shift: 1)" /> <!-- 12 because each bit moves one place left -->
<let name="halved" value="$permissions->bits(shift: -1)" /> <!-- 3 because a negative shift moves right -->
<let name="flipped" value="$permissions->bits(invert)" /> <!-- -7 because every bit flips -->$read = 4
$write = 2
$permissions = 6 // read and write, because 4 + 2 is 6
$can_read = $permissions->bits(and: $read) == $read // true because 6 has the 4 bit
$with_run = $permissions->bits(or: 1) // 7 because or: adds the 1 bit
$toggled = $permissions->bits(xor: $write) // 4 because xor: flips the 2 bit off
$doubled = $permissions->bits(shift: 1) // 12 because each bit moves one place left
$halved = $permissions->bits(shift: -1) // 3 because a negative shift moves right
$flipped = $permissions->bits(invert) // -7 because every bit flips单独的 &、单独的 |、<< 以及位于两个值之间的 >> 都是错误,错误信息会指出应改用的 ->bits() 调用。& 和 | 会给出修复建议,写成 and 或 or,这通常才是原意。
($hour + 24) % 24 是旧的数值回绕写法。KS6021 建议改用 $hour %% 24,它同样适用于小于 -24 的数值。
+=、-=、*=、/=、~/=、%=、%%=、**= 和 ??= 会就地修改变量:$total += 5 等同于 $total = $total + 5。运算符两侧的空格由你决定,但运算符内部的空格会将其拆分:2 * * 10 是一个错误,错误信息会指出 **,其快速修复会删除该空格。
编辑器会在运算符和空格之后列出变量,左侧类型的变量排在前面;将鼠标悬停在运算符上,可以看到它的作用。
使用 expression as Type 进行带检查的类型转换,例如 $text as int。目标类型也可以是已声明的记录、枚举或精化类型。带检查的类型转换