Skip to main content

Local variable

Instructions presented below declare a local variable in entrypoints' effect section.


A local variable is declared by the var keyword followed by the variable identifier and the initial value.

For example, the following declares a variable counter with initial value 0:

var counter = 0

It is possible to specify the variable type:

var counter : nat = 0

Specifying the type is ususally optional as the typer may infer the type of the initial value. It is mandatory though for some types like or, or some literals like none or [].


Similar to var except that a const local variable cannot be modified by an assignment instruction.

For example, the following instructions generates a compilation error:

const amount = 10tz;
amount += 1tz /* compilation error */

?= (get option, assign or fail)

It is possible to declare a local variable (const or var) as the some value of an option variable, and fail with an error message if this variable is none.

For example, the following declares a const local variable as the some value of the execution of unpack buitlin which returns an option value:

const v ?= unpack<nat>(0x0505)

By default, it fails with "OPTION_IS_NONE".

It is equivalent to:

const v = 0;
match unpack<nat>(0x0505) with
| some(u) -> v := u
| none -> fail("OPTION_IS_NONE")

Explicit error

It is possible to declare an explicit error message after the : character. The error message may be any packable value (not just a string).

For example, suppose m is a map of type map<T, nat>:

var v ?= m[k] : ("KEY_NOT_FOUND", k)

The above declaration fails with pair ("KEY_NOT_FOUND", k). It is equivalent to:

var v = 0;
match m[k] with
| some(u) -> v := u
| none -> fail(("KEY_NOT_FOUND", k))