Skip to content
kata / a language workbench

Control flow and patterns

if, elif, and else select a branch. Because the construct is an expression, it can initialize a value.

let score = 7
let label = if score >= 9 {
"high"
} elif score >= 5 {
"middle"
} else {
"low"
}
print(label)

Conditions use truthiness. A missing else yields nil when no branch matches.

let n = 0
while n < 5 {
n = n + 1
if n == 2 { cont }
if n == 4 { bail }
print(n)
}

This prints 1 and 3. cont starts the next iteration; bail exits the nearest loop. Advance your loop state before a possible cont so the condition can eventually change.

for (name, score) in [("Ada", 42), ("Lin", 37)] {
print("{name}: {score}")
}

The iterable supplies to_iter(); the iterator supplies next(), returning Opt.Val(value) or Opt.Non. Tuple patterns unpack each yielded value.

There is no built-in range expression yet. Use while, an array, or a custom iterator. Do not grow or otherwise mutate a collection while iterating over it.

let value = Opt[Int].Val(42)
let label = match value {
Val(0) -> "zero",
Val(n) -> "value: {n}",
Non() -> "absent",
}
print(label)

Arms use -> and can return expressions or execute a braced body. Patterns can be literals, bindings, _, tuples, or enum variants. They can nest, as Val(0) does above.

Variant patterns always have parentheses, including variants without payloads. Non() matches a variant; an ordinary bare name binds the subject.

KataScript does not provide exhaustive-match checking, guards, or alternative patterns joined with |. Cover every expected case yourself. A wildcard makes sense only when ignoring the remaining cases is intentional.