Skip to main content
  • Use val for immutable variables and var for mutable variables.
  • Use const or global outside functions.

Declaration

  • val declares a variable that is assigned exactly once:
  • var declares a variable that can be reassigned:

Explicit types

A variable may declare its type explicitly. If the type is not specified, it is inferred from the initial assignment:
Explicit types also can be used with structures:

Unassigned variables

A variable may be declared without an initial value, but it must be assigned before its first use:

Multiple variables

Declaring multiple variables at once is tensor destructuring:

Nested scope

A block { ... } introduces a nested scope:

Function parameters

Function parameters behave the same as local variables. They can be reassigned, but the reassignment does not affect the caller’s state:
To make updates to userId visible in demo, declare the parameter with mutate: mutate userId.

Constants

Global-scope constants are declared with const outside functions:
The right-hand side must be a constant expression: numbers, const literals, compile-time functions, etc.
The type is inferred unless explicitly specified:
Constants are not limited to integers:
To calculate CRC32 and similar values at compile time, use "...".crc32() and similar compile-time methods. To group integer constants, use enums.

Global variables

Use the global keyword to declare variables outside functions:
A global must have an explicit type and cannot be initialized at the point of declaration. Initialization is done manually at some point in a program. A contract has several entry points, such as get fun, onInternalMessage, and others. A global must be initialized along the execution path where it is required.
In Tolk, avoid globals when possible. Use auto-serialization and lazy loading.