Skip to main content
Variables are declared with val (immutable) or var (mutable). Outside functions, use const or (rarely) global.

Keywords val and var

val declares a variable that is assigned exactly once (immutable):
var declares a variable that may be reassigned:
Explicit types can be specified. If not specified, the variable type is inferred from the initial assignment:
For instance, explicit types are useful for structures:
A variable may be left unassigned at declaration. Then it must be definitely assigned before its first use:
Creating multiple variables at once is actually destructuring of a tensor:
The block { ... } opens a nested scope:

Parameters of a function

Function parameters work exactly like local variables. They can be reassigned, but changes do not affect the caller’s state:
To make modifications to userId visible inside demo, the parameter must be declared as mutate userId. See mutability.

Constants

Global-scope constants are declared with const outside functions:
The right side of an assignment must be a constant expression: numbers, const literals, compile-time functions, etc.
The type is inferred from assignment unless specified manually:
Constants are not restricted to integers:
To calculate crc32 / etc. at compile-time, use stringCrc32("...") and similar. See strings. To group integer constants, also use enums.

Global variables

Tolk has the global keyword to declare variables outside functions:
It must be followed by a type but cannot be initialized at the point of declaration: initialization is done manually at some point of a program. A contract has several entrypoints (get fun, onInternalMessage, and more low-level). So, a particular global must be initialized at some place where its forward usage is expected.