Skip to main content

Control

In addition of being instructions, if and match with also come as conditional expressions.

if then else

Expression if e then a else b evaluates to the evaluation of expression a if boolean expression e evaluates to true, and to the evaluation of b otherwise.

a and b must be of same type.

For example:

const r = if now > deadline then 10% else 2%

match with

Expression match with deconstructs a value of enumerated type to extract data from it.

Like match with instruction, enumerated types are option, or, list, states and composite type enum.

For example on a option value prize of type option<tez>:

const fee =
match prize with
| some(v) -> 20% * v
| none -> 5tz
end

See the instruction equivalent for examples of desconstruction on the other enumerated types.

As for if expression above, expressions for each enumerated value must be of same type.

a ? b : c

Bool

When a is of type bool, expression a ? b : c is a synonym of if a then b else c.

For example:

const ratio = balance > threshold ? 5% : 10%

Option

When a is of type option, expression a ? b : c is another syntax to fold the optional value a, where:

  • b is the expression to evaluate in case a is some value
  • c is the expression to evaluate in case a is none

The the keyword is used in b to refer to the argument of the some value.

For example, if prize is of type option<tez>:

const reward = prize ? 20% * the : 5tz

It is equivalent to:

const reward =
match prize with
| some(v) -> 20% * v
| none -> 5tz
end