Skip to main content
Tolk provides constructs for controlling contract flow.
  • Use if, assert, and loops for conditional logic.
  • Use match expression for pattern matching.

if statement

if statement works as in most languages. else if and else blocks are optional.
A condition must be a boolean or an integer. If not equals 0, then true:
The body of if and else must be enclosed in { ... }:

assert statement

assert throws an exceptions if a condition is false.
It is equivalent to the following form:

match expression

match is used to perform different actions for different values of a variable. A common use case is routing values of a union type:
The match is equivalent to a series of if-else checks:
The match can also be used for expressions, switch-like behavior:

Ternary operator

The ternary form condition ? when_true : when_false is available:
If the types of when_true and when_false differ, the result becomes a union type. In most cases this is unintended, so the compiler reports an error.

while loops

while and do-while loops repeatedly execute their bodies while the condition remains true. while checks the condition first and may not execute the body at all, whereas do-while runs the body first.
while is used to iterate over maps:

repeat loop

The repeat (N) statement executes its block N times:
N may be either a constant or a variable.

break and continue

The keywords break and continue are not supported.