Skip to main content

Variable declarations

In Motoko, variables are declared using:

  • let for immutable bindings: Once assigned, the value cannot be changed.

  • var for mutable bindings: The value can be updated later.

Immutable variables

The let keyword is used to bind a name to the result of an expression, creating an immutable variable. In a let declaration, the expression on the right-hand side is evaluated once, and its resulting value is associated with the given name. This value cannot be changed after the initial assignment. The Motoko compiler enforces this immutability by generating a compile-time error if there is any attempt to modify the variable later in the code.

In concurrent code, immutable declarations should be preferred over mutable declarations, since they prevent concurrent modification.

let x = 10;
x := 20; // Error: Cannot assign to immutable variable

The left hand side of a let can be also be a more general pattern, naming the components of a value by matching its structure:

For example, the declaration:

let (fst, snd) = (1, 2); 

uses the pattern (fst, snd) to name the components of the pair (1,2). The value of fst is 1 and the value of snd is 2. Both fst and snd are immutable.

Mutable variables

A var declaration in Motoko defines a mutable variable whose value can be updated after it is initially assigned. Unlike let, which creates an immutable binding, var allows reassignment using the := operator. This means that the variable can hold different values over time, making it suitable for scenarios where state changes are required.

var y = 10;
y := 20; // Allowed, updates the value of y

Unlike, let declarations, var declarations do not support pattern matching. For example, the following is a syntax error:

var (a, b) = (1, 2); // Not supported