Skip to main content

Imports

Imports must appear at the top of the file:
In most workflows, the IDE adds imports automatically. For example, when selecting an item from auto-completion. The entire file is imported. There are no modules or exports; all symbols must have unique names within the project.

Structures

A struct Point holding two 8-bit integers:
  • Methods are declared as fun Point.method(self).
  • Fields can use any types: numeric, cell, union, and others.
  • Fields can define 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:

Functions

A function that returns the sum of two integers:
  • Parameter types are mandatory.
  • The return type can be omitted: it is auto-inferred.
  • Parameters can define default values: fun f(b: int = 0)
  • Statements in a block are separated by semicolons ;.
  • Generic functions are supported: fun f<T>(value: T) { ... }
  • Assembler functions are supported: fun f(...): int asm "..."

Methods

A function declared as fun <receiver>.name(...) is a method.
  • If the first parameter is self, it’s an instance method.
  • If the first parameter is not self, it’s a static method.
By default, self is immutable; mutate self allows modifying the object. Methods can be declared for any type, including primitives:

Variables

Within functions, variables are declared with val or var keywords. The val keyword declares an immutable variable that can be assigned only once:
The var keyword declares a variable that can be reassigned:
A variable’s type can be specified after its name:
Declaring variables at the top level, outside functions, is supported using the global keyword.

Constants

Constants can be declared only at the top level, not inside functions:
To group integer constants, enums are useful.

Value semantics

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

Semicolons

  • Semicolons are optional at the top level, after imports, aliases, etc.
  • Semicolons are required between statements in a function.
  • After the last statement in a block, a semicolon is optional.

Comments

Tolk supports single-line or end-of-line and multi-line or block comments:

Conditional operators

In conditions, if is a statement. else if and else blocks are optional.
A ternary operator is also available:

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:
Union types are commonly used when handling incoming messages.

While loop

Tolk does not have a for loop; use while loop for repeated execution.

Assert and throw

The try-catch statement is supported for exceptions, although it is not commonly used in contracts.

Arrays

Arrays are dynamically sized containers created with [...]:

Iterate over a map

To iterate, maps can be used:

Send a message to another contract

To construct and send a message, a message body is typically represented by a structure. For example, RequestedInfo:

Contract getters

Contract getters or get-methods are declared with get fun: