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;
}