Skip to main content
Version: Next

Variables and constants

LIGO features mutable variables, that is, variables whose values can be reassigned. By contrast, constants can be assigned only once.

Variables

To declare a variable, use the keyword let instead of the keyword const, which is for constants. To assign new values to the variable, use the = operator, as in this example:

function add (a: int, b:int) : int {
let c = a + b; // not const!
c++; // Reassignment of incremented c
return c; // c == a + b + 1
};

Silent variables

The compiler warns you when you declare a variable but do not use it. To ignore a variable, turn it into a silent variable by prefixing its name with an underscore or using an underscore as its name.

For example, LIGO entrypoints receive a parameter and the current value of storage as arguments. If the entrypoint code doesn't access one or both of these arguments, prefix the argument name with an underscore to hide the compiler warning, as in this example:

@entry
const reset = (_param : unit, _storage : int) : [list<operation>, int] => [[], 0];

Constants

Constant values cannot be reassigned after they are declared.

To declare a constant, use the keyword const, as in this example:

const a = 1;
const b : int = a; // Type ascription (a.k.a. annotation)

You cannot assign a new value to a constant in the same scope:

const x = 1;
const x = 2; // Yields an error

However, the following example works because the constants are in different scopes:

const d = do {
const x = 1;
{
const x = 2; // No error: a sub-block
return x;
}
};