Skip to main content
Tolk provides flexible constructs for controlling contract flow. Use if, assert, and loops to express conditional logic. The match expression is a powerful “pattern-matching” feature.

if statement

Similar to common languages, optionally followed by else if or else.
A condition must be a boolean or an integer (will be true if not equals 0):
The body of if and else must be enclosed in { ... }:

assert statement

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

match expression

match is used to perform different actions for different values of a variable. One 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):
See union types and pattern matching.

Ternary operator

A ternary condition ? when_true : when_false is also available:
Note that if types of when_true and when_false are different, a resulting type is a union. However, this is typically not the intended result, 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.
For instance, while is used to iterate over maps:

repeat loop

The repeat (N) statement executes a block N times:
N does not have to be a constant; it may also be a variable.

Break and continue in loops

The keywords break and continue are not implemented yet, and a for loop may be added in future versions.