Imports
Imports must appear at the top of the file:Structures
A structPoint 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
privateandreadonly. - Structs can be generic:
struct Wrapper<T> { ... }.
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 asfun <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.
self is immutable; mutate self allows modifying the object.
Methods can be declared for any type, including primitives:
Variables
Within functions, variables are declared withval or var keywords. The val keyword declares an immutable variable that can be assigned only once:
var keyword declares a variable that can be reassigned:
global keyword.
Constants
Constants can be declared only at the top level, not inside functions: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.
Union types and matching
Union types allow a variable to hold one of possible types. They are typically handled bymatch:
is or !is operators:
While loop
Tolk does not have afor loop; use while loop for repeated execution.
Assert and throw
Thetry-catch statement is supported for exceptions, although it is not commonly used in contracts.
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 withget fun: