Skip to main content
Syntax of Tolk is similar to TypeScript, Rust, and Kotlin. It is designed to be straightforward to read and write. Below is a list of basic syntax elements with examples. Most sections end with a link to a detailed topic.

Imports

Imports exist at the top of the file:
In typical workflows, an IDE inserts imports automatically (for example, when selecting an element from auto‑completion). See: imports.

Structures

A struct Point holding two 8-bit integers:
  • methods are declared like fun Point.method(self), read below
  • fields can be of any types: numeric, cell, union, etc. (see type system)
  • fields can have default values: x: int8 = 0
  • fields can be private and readonly
  • structs can be generic: struct Wrapper<T> { ... }
If all fields are serializable, a struct can be automatically serialized:
See: structures.

Functions

A function that calculates the sum of two integers:
  • parameter types are mandatory
  • the return type can be omitted: it will be auto-inferred, like in TypeScript
  • parameters can have a default value: fun f(b: int = 0)
  • statements inside a block are separated by semicolons ;
  • generic functions: fun f<T>(value: T) { ... }
  • assembler functions: fun f(...): int asm "..."
See: functions and methods.

Methods

A function declared as fun <receiver>.name(...) is a method.
  • if the first parameter is self, it’s an instance method
  • if not self, it’s a static method
  • by default, self is immutable, but mutate self allows modifying an object
  • methods may be declared not only for a struct, but for any type, even a primitive:
See: functions and methods.

Variables

Inside functions, variables are declared with val or var keywords. The val keyword declares a variable that is assigned exactly once (immutable):
The var keyword declares a variable that may be reassigned:
Variable’s type can be specified after its name:
Declaring variables at the top-level (not inside functions) is supported via global keyword. See: variables.

Constants

Declaring constants is allowed at the top-level (not inside functions):
To group integer constants, enums are also useful.

Value semantics

Tolk follows value semantics: assignments create independent copies, and function calls do not mutate arguments unless explicitly specified.
See: mutability.

Semicolons

  • semicolons are optional at the top-level (after imports, aliases, etc.)
  • required between statements in a function
  • after the last statement in a block, it’s also optional

Comments

Like most modern languages, Tolk supports single-line (or end-of-line) and multi-line (block) comments:

Conditional operators

In Tolk, if is a statement, with else if and else optional blocks. A ternary operator is also available:
See: conditions and loops.

Union types and matching

Union types allow a variable to hold “one of possible types”. They are typically handled by match:
Alternatively, test a union with is or !is operators:
Unions types are commonly used when handling incoming messages. See: union types.

While loop

The for loop does not exist. See: conditions and loops.

Assert and throw

A try-catch statement is also supported, although it is not commonly used in contracts. See: exceptions.

Iterate over a map

See: maps.

Send a message to another contract

An outgoing message body is typically represented by a structure (for example, RequestedInfo).
See: constructing and sending messages.

Contract getters

Contract getters (or “get methods”) are declared with get fun:
See: contract getters.