Skip to content
Konjure / spatial intelligence

Language basics

Learn the language by running and changing small programs.

Programs live in .kj files. Edit any example and press Run to compile it and see its output. Each example runs independently.

Values

Write a type after :. () is the value of Unit; List[Number] holds numbers. Semicolons end statements, and // starts a comment.

Tick 0 · 0.00 s⌘ / Ctrl + EnterThis device
main.kj
Loading the language runtime…
Output 0
No output.

Try changing 7 to 2.5. Number supports fractions as well as whole numbers. Replace it with "seven" to see a type error.

See Number for the compatibility alias, Numbers, text and bytes for exact widths, and Tensors and media for shaped arrays.

Bindings

let fixes a binding; let mut lets you assign another value of the same type. A local binding can infer its type from the first value.

Tick 0 · 0.00 s⌘ / Ctrl + EnterThis device
main.kj
Loading the language runtime…
Output 0
No output.

Try changing count = 3 to count = count + 5: the first output becomes 6. Assigning true instead fails compilation; mutability does not change the type.

Operators

Use arithmetic to calculate, comparisons to produce a Bool, and and, or, or ! to combine or negate conditions. Parentheses control grouping.

Tick 0 · 0.00 s⌘ / Ctrl + EnterThis device
main.kj
Loading the language runtime…
Output 0
No output.

Try removing the parentheses: 2 + 3 * 4 produces 14 instead of 20. Try true and false, then true or false: compare false and true. Both operators short-circuit: false and expression and true or expression skip the expression on the right. and binds more tightly than or.

Functions

func names a function. Parameters and the return type are explicit; ret value; returns a result, while ret; exits a Unit function.

Tick 0 · 0.00 s⌘ / Ctrl + EnterThis device
main.kj
Loading the language runtime…
Output 0
No output.

Try double(5) to get 10. double(true) is a compile error because x requires a Number.

Conditions

if chooses a branch using a Bool. else handles the other case.

Tick 0 · 0.00 s⌘ / Ctrl + EnterThis device
main.kj
Loading the language runtime…
Output 0
No output.

Try a score of 3: the output changes from pass to try again. if score { ... } would be invalid; numbers are not Boolean conditions.

Loops

For each value

for value in collection visits a list in order. continue skips to the next iteration; break exits the loop. This example prints 1 and 3.

Tick 0 · 0.00 s⌘ / Ctrl + EnterThis device
main.kj
Loading the language runtime…
Output 0
No output.

Try changing value == 2 to value == 3: the output becomes 1 and 2.

While a condition holds

while checks its condition before each iteration. This countdown prints 3, 2, 1, then done.

Tick 0 · 0.00 s⌘ / Ctrl + EnterThis device
main.kj
Loading the language runtime…
Output 0
No output.
remaining > 0falseprint("done")trueprint(remaining)remaining = remaining - 1remaining > 0trueprint(remaining)remaining = remaining - 1falseprint("done")

Try starting at 0: only done prints. Without the subtraction the loop cannot finish; the runtime stops it when its execution budget runs out.

Lists

Index from zero with values[0]. append returns a new list, and len counts its entries. An empty list needs an element type.

Tick 0 · 0.00 s⌘ / Ctrl + EnterThis device
main.kj
Loading the language runtime…
Output 0
No output.

Try values[1] to print 4. values[2] fails at runtime: appending created added, leaving the two entries in values unchanged.

Continue with Types and traits to give values fields and methods. Exact syntax and type rules live in the language reference; available helpers are in the standard library.