KScript docs
EN

The language

Expressions

+ - * / % **, == != (loose: '3' == 3), === !== (strict), < <= > >=, and or not, ??, ?., a ? b : c, in, list [1, 2] and map { name: 'Joan' } literals (a list becomes a set with as set), $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.

When comparing a number with a string using <, <=, > or >=, KScript compares numerically if the string can be parsed as a number. If parsing fails, the comparison returns false. Comparisons chain as in maths, which the Maths section shows. Two dates are equal with == only when they are the same moment, seconds included.

Maths

Everyday maths has its own symbol, and each works the same in a script, in a template and in a database query. + adds, - subtracts, * multiplies and / divides, keeping any fraction. ~/ divides into whole numbers, rounding down. % gives the remainder, which keeps the sign of the number on the left, %% wraps round like a clock, and ** raises to a power:

<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

Operators bind in this order, tightest first, so 2 + 3 * 4 is 14 because * binds before +:

Level Operators Grouping
1 . -> ?. ( ) [ ] left to right
2 ** right to left: 2 ** 3 ** 2 is 2 ** 9, 512
3 -x +x -2 ** 2 is -4 because ** binds first, so write -(2 ** 2) or (-2) ** 2 (KS6023)
4 * / ~/ % %% left to right
5 + - left to right
6 .. ..= step one range per expression
7 |> left to right
8 < <= > >= in a chain in one direction: 0 <= $n < 10
9 == != === !== one per comparison: join more with and
10 not, and, or, ??, a ? b : c

% and %% only differ when a sign is negative. The remainder keeps the sign of the left side, like C# and JavaScript. Mod takes the sign of the right side, so it wraps round: an hour before midnight is 23, not -1. Use %% for clocks, weekdays and positions in a list that wrap at the end:

<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

~/ rounds down, not toward zero, so it pairs with %%: ($a ~/ $b) * $b + $a %% $b is always $a. It works on durations and money too, and gives a plain number:

<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

// starts a comment, so it can't be whole-number division. $hours = $minutes // $per_hour leaves $minutes alone, and KS6022 points it out with a fix that writes ~/.

Comparisons that point the same way chain as they do in maths, so a range check reads the way it is said. Each value in the middle is worked out once, and the chain stops at the first comparison that is false:

<let name="score" value="42" />
<let name="passed" value="0 &lt;= $score &lt; 100" /> <!-- true because 42 is from 0 up to (not including) 100 -->
<let name="top" value="90 &lt;= $score &lt;= 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

A chain goes one way. 1 < $n > 0 is an error because it says nothing about how 1 and 0 compare, so write 1 < $n and $n > 0. == and != don't chain either: $a == $b == $c would compare true or false with $c, so write $a == $b and $b == $c. Brackets stop a chain, so (1 < $n) == true compares the result. A chain works in a database query too: Person->where(fn(person) => 18 <= $person.age < 65) asks for age >= 18 AND age < 65. ->between() is gone, and its error names the chain to write instead.

.. makes a range that stops before its end, which suits counting from 0 and positions in a list. ..= takes in the end as well, which suits counting from 1. Hovering either one shows where the range starts and stops:

<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

Like .., a range counts down when its end is below its start, so 3..=1 is [3, 2, 1] and 1..=0 is [1, 0]. To count up to a value that may be 0, as in 1..($count + 1), keep .., which gives no values when $count is 0.

^ isn't power in KScript (in C# and JavaScript it is a bitwise operator), so 2 ^ 10 is an error whose fix writes 2 ** 10.

Bits are worked on with one method, ->bits(), rather than symbols, because & and | are so easily mistaken for and and or. It takes one step at a time, on whole numbers:

<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

&, a single |, << and >> between two values are errors that name the ->bits() call to write, and & and | offer a fix that writes and or or, which is usually what was meant.

($hour + 24) % 24 was the old way to wrap a number round. KS6021 suggests $hour %% 24, which also works for numbers below -24.

+=, -=, *=, /=, ~/=, %=, %%=, **= and ??= change a variable in place: $total += 5 is $total = $total + 5. Spaces around an operator are up to you, but a space inside one splits it: 2 * * 10 is an error that names **, and its quick fix removes the space.

The editor lists the variables after an operator and a space, those of the left side's type first, and hovering an operator shows what it does.

Use expression as Type for a checked conversion, for example $text as int. The target can also be a declared record, enum or refinement type. Checked conversions