Imports
Imports exist at the top of the file:Structures
A structPoint holding two 8-bit integers:
- methods are declared like
fun Point.method(self), read below - fields can have any types: numeric, cell, union, etc. (see type system)
- default values for fields:
x: int8 = 0 - fields can be
privateandreadonly - generic structs are supported:
struct Wrapper<T> { ... }
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
- default values supported:
fun f(b: int = 0) - statements inside a block are separated by semicolons
; - generic functions supported:
fun f<T>(value: T) { ... } - assembler functions supported:
fun f(...): int asm "..."
Methods
A function declaredfun <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,
selfis immutable, butmutate selfallows modifying an object - methods may be declared not only for a struct, but for any type, even a primitive:
Variables
Inside functions, variables are declared withval or var keywords.
The val keyword declares a variable that is assigned exactly once (immutable):
var keyword declares a variable that may be reassigned:
global keyword.
See: variables.
Constants
Declaring constants is allowed 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.)
- 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
if is a statement, with else if and else optional blocks.
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 bymatch:
is or !is operators:
While loop
for loop does not exist.
See: conditions and loops.
Assert and throw
Iterate over a map
Send a message to another contract
An outgoing message body is typically represented by a structure (for example,RequestedInfo).
Contract getters
Contract getters (or “get methods”) are declared withget fun: