Skip to main content

Basic syntax

Comments

Tolk uses traditional C-style syntax for comments:

Identifiers

Identifiers must start with [a-zA-Z$_] — that is, a letter, underscore, or dollar sign — and may continue with characters from [a-zA-Z0-9$_]. As in most programming languages, camelCase is the preferred naming convention.

Functions

Function declaration

Use the fun keyword with TypeScript-like syntax:
Examples:
If the return type is omitted, it’s auto-inferred.

Generic functions

Tolk supports generic functions:

Default parameters

Function parameters can have default values:

GET methods

Contract getters use the get fun syntax:

Variables and types

Variable declaration

Declare variables with var for mutable variables and val for immutable ones:
Type annotations are optional for local variables:

Variable scope

Variables cannot be redeclared within the same scope:

Control flow

Conditional statements

Loops

While loop

Do-while loop

Repeat loop

Type system

Basic types

  • int - integer; fixed-width variants like int32 and uint64 are also supported
  • bool - boolean (true/false). Note: in TVM, true is represented as -1, not 1
  • cell - a TVM cell
  • slice - a TVM slice; you can also use bitsN. (e.g. bits512 for storing a signature)
  • builder - a TVM builder
  • address - internal (standard) address
  • any_address — internal/external/none address
  • coins - Toncoin or coin amounts
  • void - no return value
  • never - function never returns (always throws an exception)

Fixed-width integers

Boolean type

The Boolean type is distinct from integers:

Nullable types

Nullable types are denoted by ?:

Union types

Union types allow a value to have multiple possible types. Typically, you handle them using match by type:
Alternatively, you can test a union value using the is or !is operators:

Tuple types

Tuples with explicit types:

Tensor types

Tensors are similar to tuples but stored on the stack:

Type aliases

Create aliases to improve code clarity:

Address type

A dedicated type for blockchain addresses:

Tensors

Indexed access

Access elements using dot . notation:

Tuples

Error handling

Throw and assert

Simplified error handling:

Try-catch

Structures

Struct declaration

Creating objects

Default values

Generic structures

Field modifiers

  • private field — accessible only within methods
  • readonly field — immutable after object creation

Methods

Instance methods

Methods are implemented as extension functions that accept self:

Mutating methods

Use the mutate keyword for methods that modify the receiver:

Methods for any type

Chaining methods

Methods can return self to enable method chaining:

Enums

They are:
  • similar to TypeScript/C++ enums
  • distinct type, not just int
  • checked on deserialization
  • allowed in throw and assert

Enums syntax

Enum members can be separated by , or by ; or by a newline — like struct fields. Like in TypeScript and C++, you can manually specify a value, the following will be auto-calculated.

Enums usage: they are distinct types

Since enums are types, you can
  • declare variables and parameters
  • declare methods for an enum
  • use them in struct fields, in unions, in generics, etc.
Enums are integers under the hood. They can be cast to int and back with as operator.

Serialization of enums

You can manually specify :intN. Otherwise, the compiler will auto-calculate it.
On deserialization, an input value is checked for correctness. E.g., for Role, if input<0 or input>2, an exception “5 (integer out of range)” will be thrown.

Imports and modules

Import syntax

Advanced features

TVM assembler functions

You can implement functions directly in TVM assembler:

Function attributes

Functions can have attributes using the @ syntax:

Trailing commas

Tolk supports trailing commas in tensors, tuples, function calls, and parameters:

Optional semicolons

Semicolons are optional for the last statement in a block:
Semicolons are also optional for top-level declarations such as constants, type aliases, and struct fields.

Compile-time functions

String-processing functions execute at compile time:
Available compile-time functions include: stringCrc32, stringCrc16, stringSha256, stringSha256_32, stringHexToSlice, and stringToBase256.

Toncoin amounts

Use human-readable Toncoin amounts:

Smart casts

Automatic type narrowing:

Non-null assertion

Use ! when you are sure a value is not null:

Auto-packing

Structures can be automatically packed to and unpacked from cells:
Deep dive: Auto-packing.

”Lazy loading” from cells: unpack only requested fields

Deep dive: Lazy loading.

Universal message composition

Create messages using a high-level syntax:
Deep dive: Sending messages.

Standard library

Common functions are available by default:
While more specific ones require importing (IDE suggests you):