式
+ - * / % **, == != (loose: '3' == 3), === !== (strict), < <= > >=, and or not, ??, ?., a ? b : c, in, list [1, 2] and map { name: 'Joan' } literals, $p.name, $list[0] (negative indexes count from the end), ranges 0..10 and 0..100 step 5 (end excluded, also with a step such as 0.1) and 1..=10 (end included), match $x { 1 => 'one', _ => 'other' }, ->method(), |> and fn(x) => …. Write and and or to combine conditions. Prefix ! has the same precedence and truthiness as not, so !$ready, ! $ready and not $ready are equivalent. Use it on a field (!$user.active), a whole condition (if !($ready and $open) { … }), or an interpolation ("{ ! null->is_null() }"). The spelling null->!is_null() negates the method result before the next link in the chain. XML has a word for each comparison as well (lt le gt ge eq ne), for use inside quoted attributes, since XML can't hold a raw < there; the shorthand start tags accept < and > as written. In a .kscript file the words are reported (KS6012) and kscript check --fix writes the symbols.
<、<=、>、>=で数値と文字列を比較する場合、文字列を数値として解析できれば、KScriptは数値として比較します。解析に失敗すると、比較はfalseを返します。比較は数学と同様に連鎖します(数学の節で示されます)。2つの日付は、==で比較したときに、秒まで含めて同じ瞬間の場合にのみ等しくなります。
数式
日常的な計算には独自の記号があり、それぞれスクリプト、テンプレート、データベースクエリで同じように動作します。+は加算、-は減算、*は乗算、/は除算で、小数を保持します。~/は整数の除算で、切り捨てます。%は余りを返し、左側の数の符号を保ちます。%%は時計のように循環します。**は累乗です:
<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 |
式ごとに範囲は1つ |
| 7 | |> |
左から右 |
| 8 | < <= > >= in |
一方向の連鎖: 0 <= $n < 10 |
| 9 | == != === !== |
比較ごとに1つ。andでさらに連結します |
| 10 | not, and, or, ??, a ? b : c |
%と%%は、符号が負の場合にのみ異なります。余りはC#やJavaScriptと同様に、左辺の符号を保ちます。Modは右辺の符号を取るため、循環します。真夜中の1時間前は-1ではなく23です。時計、曜日、末尾で循環するリストの位置には%%を使います:
<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~/は0に向かってではなく、切り捨てで丸めます。そのため%%と組み合わせます。($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と記述します。
ビットは記号ではなく、1つのメソッド->bits()で操作します。&や|はandやorと簡単に取り違えられてしまうためです。整数に対して1ステップずつ処理します:
<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&、単一の|、<<、>> で2つの値の間に置かれたものはエラーで、書き込むべき->bits()呼び出しを示します。&と|はandまたはorを書き込む修正を提案します。これは通常、意図されていたものです。
($hour + 24) % 24は、数値を折り返す古い方法です。KS6021では$hour %% 24の使用を推奨しています。これは-24より小さい数値にも対応しています。
+=、-=、*=、/=、~/=、%=、%%=、**=、??= は変数をその場で変更します。$total += 5 は $total = $total + 5 です。演算子の前後のスペースは任意ですが、演算子の内側にスペースを入れると演算子が分割されます。2 * * 10 は ** という名前を示すエラーになり、その簡単な修正方法はスペースを削除することです。
エディターは、演算子の後にスペースを入れると、左辺の型に一致する変数を先頭にして変数の一覧を表示します。また、演算子にカーソルを合わせると、その演算子の働きが表示されます。
チェック付き変換には expression as Type を使用します。例えば $text as int のようにします。ターゲットとして、宣言済みのレコード型、列挙型、または精製型を指定することもできます。チェック付き変換