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 thefun keyword with TypeScript-like syntax:
Generic functions
Tolk supports generic functions:Default parameters
Function parameters can have default values:GET methods
Contract getters use theget fun syntax:
Variables and types
Variable declaration
Declare variables withvar for mutable variables and val for immutable ones:
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 likeint32anduint64are also supportedbool- boolean (true/false). Note: in TVM,trueis represented as-1, not1cell- a TVM cellslice- a TVM slice; you can also usebitsN. (e.g.bits512for storing a signature)builder- a TVM builderaddress- blockchain addresscoins- Toncoin or coin amountsvoid- no return valuenever- 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 usingmatch by type:
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
privatefield — accessible only within methodsreadonlyfield — immutable after object creation
Methods
Instance methods
Methods are implemented as extension functions that acceptself:
Mutating methods
Use themutate keyword for methods that modify the receiver:
Methods for any type
Chaining methods
Methods can returnself to enable method chaining:
Enums
- similar to TypeScript/C++ enums
- distinct type, not just
int - checked on deserialization
- allowed in
throwandassert
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.
int and back with as operator.
Serialization of enums
You can manually specify:intN. Otherwise, the compiler will auto-calculate it.
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:Compile-time functions
String-processing functions execute at compile time: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: