Operators & Expressions
Operators
Operators in Leo compute a value based off of one or more expressions. Leo defaults to checked arithmetic, which means that it will throw an error if an overflow or division by zero is detected.
For instance, addition adds first with second, storing the outcome in destination. For integer types, a constraint is added to check for overflow.
For cases where wrapping semantics are needed for integer types, see the wrapped variants of the operators.
let a: u8 = 1u8 + 1u8;
// a is equal to 2
a += 1u8;
// a is now equal to 3
a = a.add(1u8);
// a is now equal to 4
note
The Leo operators compile down to Aleo Instructions opcodes executable by the Aleo Virtual Machine (AVM).
Operator Precedence
Operators will prioritize evaluation according to:
| Operator | Associativity |
|---|---|
! -(unary) | |
** | right to left |
* / | left to right |
+ -(binary) | left to right |
<< >> | left to right |
& | left to right |
| | left to right |
^ | left to right |
< > <= >= | |
== != | left to right |
&& | left to right |
|| | left to right |
= += -= *= /= %= **= <<= >>= &= |= ^= |
Parentheses
To prioritize a different evaluation, use parentheses () around the expression.
let result = (a + 1u8) * 2u8;
(a + 1u8) will be evaluated before multiplying by two * 2u8.