Language guide
Table of contents
- Basic syntax
- Functions
- Variables and types
- Control flow
- Type system
- Collections
- Error handling
- Structures
- Methods
- Imports and modules
- Advanced features
- Standard library
Basic syntax
Comments
Tolk uses traditional C-style syntax for comments:
// Single-line comment
/*
Multi-line comment
can span multiple lines
*/
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.
var justSomeVariable = 123;
Functions
Function declaration
Use the fun
keyword with TypeScript-like syntax:
fun functionName(param1: type1, param2: type2): returnType {
// function body
}
Examples:
fun parseData(cs: slice): cell { }
fun loadStorage(): (cell, int) { }
fun main() { ... }
If the return type is omitted, it's auto-inferred.
Generic functions
Tolk supports generic functions:
fun swap<T1, T2>(a: T1, b: T2): (T2, T1) {
return (b, a);
}
Default parameters
Function parameters can have default values:
fun increment(x: int, by: int = 1): int {
return x + by;
}
GET methods
Contract getters use the get fun
syntax:
get fun seqno(): int {
return 1;
}
Variables and types
Variable declaration
Declare variables with var
for mutable variables and val
for immutable ones:
var mutableVar: int = 10;
val immutableVar: int = 20;
Type annotations are optional for local variables:
var i = 10; // int inferred
var b = beginCell(); // builder inferred
Variable scope
Variables cannot be redeclared within the same scope:
var a = 10;
var a = 20; // Error! Use: a = 20;
if (true) {
var a = 30; // OK, different scope
}
Control flow
Conditional statements
if (condition) {
// code
} else if (anotherCondition) {
// code
} else {
// code
}
Loops
While loop
while (condition) {
// code
}
Do-while loop
do {
// code
} while (condition);
Repeat loop
repeat (10) {
// body
}
Type system
Basic types
int
- integer; fixed-width variants likeint32
anduint64
are also supportedbool
- boolean (true
/false
). Note: in TVM,true
is represented as-1
, not1
cell
- a TVM cellslice
- a TVM slice; you can also usebitsN
. (e.g.bits512
for 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
var smallInt: int32 = 42;
var bigInt: uint64 = 1000000;
Boolean type
The Boolean type is distinct from integers:
var valid: bool = true;
var result: bool = (x > 0);
if (valid) { // accepts bool
// code
}
// Cast to int if needed
var intValue = valid as int; // -1 for true, 0 for false