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 comments:
// Single-line comment
/*
Multi-line comment
can span multiple lines
*/
Identifiers
Identifiers can start with [a-zA-Z$_]
and continue with [a-zA-Z0-9$_]
, like in most languages, camelCase preferred.
var justSomeVariable = 123;
Functions
Function declaration
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() { ... }
When return type 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
Parameters can have default values:
fun increment(x: int, by: int = 1): int {
return x + by;
}
GET methods
Contract getters — with get fun
:
get fun seqno(): int {
return 1;
}
Variables and types
Variable declaration
Variables are declared with var
(mutable) or val
(immutable):
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