KScript docs
EN

Debugging

The Variables view

When the script pauses, the Variables view shows what each variable holds at that moment. They are split into scopes:

  • Locals: the variables of the function the script is paused in: its parameters and what it sets. Inside a loop, the loop's own variables (such as $price and $index) are here too, even at the script's top level. Outside any function or loop there are no Locals.
  • Globals: the variables the script sets at its top level. Functions and types aren't listed, since they don't change.
  • Built-in: what the script's packages give every script, such as $args. It is left out when there is nothing in it.

Text is shown in quotes, so '7' is text and 7 is a number. The type is shown when you hover over a value. A list, a map or a record opens with the arrow next to it, so you may look inside it. A record shows its type's name:

<type name="Point">
  <field name="x" type="int" />
  <field name="y" type="int" />
</type>

<let name="corner" value="Point(x: 3, y: 4)" />
<let name="tags" value="['red', 'blue']" />
<let name="sizes" value="{ small: 1, large: 9 }" />
<!--
  Paused here, Globals shows
    $corner = Point { x: 3, y: 4 }
    $tags = ['red', 'blue']
    $sizes = { small: 1, large: 9 }
  and opening $tags shows [0] = 'red' and [1] = 'blue'.
-->
<echo>done</echo>
type Point {
  x: int
  y: int
}

$corner = Point(x: 3, y: 4)
$tags = ['red', 'blue']
$sizes = { small: 1, large: 9 }
// Paused here, Globals shows
//   $corner = Point { x: 3, y: 4 }
//   $tags = ['red', 'blue']
//   $sizes = { small: 1, large: 9 }
// and opening $tags shows [0] = 'red' and [1] = 'blue'.
echo 'done'

Click a different line in the Call Stack to see the variables of a function further up: the Variables view, the Debug Console and hovers all follow the line you pick.

Hovers, watches and the Debug Console

While the script is paused, hover over a variable in the editor to see its value. The Watch view keeps an expression's value in sight as you step, such as $basket_total or $prices->count(). The Debug Console works out any expression you type, using the variables of the paused line:

Text
> $prices->sum()
7.75
> $quantities[2] * 2
8

An expression may call functions and methods, so the debug console can try a fix before you write it into the script. Be careful with functions that change things, such as writing a file: they really run.