Operators
Almost every contract operates on data: transforming some values into others. The scope may vary, but operators lie at the core of such modifications.
This page lists all the operators in Tact in decreasing order of their precedence, with examples of usage.
Table of operators
The following table lists operators in order of decreasing precedence, from highest to lowest.
Brief description | Operators |
---|---|
Parentheses | () |
Unary postfix | !! |
Unary prefix | + - ! ~ |
Multiplicative | * / % |
Additive | + - |
Shift | >> << |
Relational | > >= < <= |
Equality | == != |
Bitwise AND | & |
Bitwise XOR | ^ |
Bitwise OR | | |
Logical AND | && |
Logical OR | || |
Ternary | ?: |
Assignment | = and all augmented assignment operators |
Precedence
All operators on this page are given in order of decreasing precedence, from highest to lowest. Precedence is used to determine which operator should be considered in a particular situation. Whenever ambiguity arises, Tact prefers operators with higher precedence over those with lower precedence.
For example, the minus sign (-
) may be considered either a subtraction operator or a negation operator, which reverses the sign of the expression from plus to minus or vice versa. As the latter has a higher precedence over the former, in cases of ambiguity between the two, Tact will first consider -
as a negation operator. Only if that interpretation does not make sense for the given expression will Tact consider it as a subtraction operator.
Consider the following code:
5 + -5; // here, the minus sign would be viewed as a negation operator5 -5; // while here it would be viewed as a subtraction operator, despite formatting
Even though this example may be simple, neglecting precedence rules can often lead to confusing situations with operators. The correct order of operations can be ensured by wrapping every operation in parentheses, since parentheses have the highest precedence of all expressions and operators.
Parentheses, ()
Parentheses (also called round brackets, ()
) are more punctuation symbols than actual operators, but their precedence is higher than the precedence of any other operator. Use parentheses to override the order of operations:
5 * 5 - 2; // 235 * (5 - 2); // 15
Unary
Unary here means that they are applied only to one operand of the given expression. All unary operators, except for the non-null assertion, have the same precedence.
Unary operators can be one of two types:
- Prefix — placed before the expression.
- Postfix (or suffix) — placed after the expression.
Non-null assert, !!
The unary double-exclamation mark (non-null assertion) operator !!
is a postfix operator that enforces non-null
values and allows direct access to the value of the optional variable if it’s not null
. Otherwise, it raises a compilation error if the compiler can track it, and if not, throws an exception with exit code 128: Null reference exception
. It can be applied to any optional variable regardless of its non-null
type.
Plus, +
Although the unary plus sign operator +
is specified in the grammar of the Tact compiler, it only exists as a binary operator.
Negate, -
The unary minus sign (negation) operator -
is a prefix operator, which reverses the sign of the expression. It can only be applied to values of type Int
:
let five: Int = 5;five + -five; // here, the minus sign is a negation operator, not a subtraction operator-(-1); // double application gives back the original value, which is 1--1; // 1
Inverse, !
The unary exclamation mark (inversion) operator !
is a prefix operator, which inverts the boolean value of the expression—changing true
to false
, and vice versa. It can only be applied to values of type Bool
:
let iLikeTact: Bool = true;!iLikeTact; // false!false; // true!(!false); // false!!false; // false
Bitwise NOT, ~
The unary tilde (bitwise NOT) operator ~
is a prefix operator, which inverts or flips each bit in the binary representation of the expression — changing each to , and vice versa. It can only be applied to values of type Int
:
let answer: Int = 42;~answer; // -43~(~answer); // 42~(~0); // 0~~0; // 0
Binary
Binary operators are split into several subsections, in order of decreasing precedence. Operators within each subsection have the same precedence as the subsection itself.
Multiplication
Multiply, divide, or obtain a remainder.
Multiply, *
The binary asterisk (multiplication) operator *
is used for multiplication of two values. It can cause integer overflows.
It can only be applied to values of type Int
:
let two: Int = 2;two * two; // 40 * 1_000_000_000; // 0-1 * 5; // -5
pow(2, 255) * pow(2, 255); // build error: integer overflow!
Divide, /
The binary slash (division) operator /
is used for integer division of two values, which truncates toward zero if the result is positive and away from zero if the result is negative. This is also called rounding down or rounding toward .
An attempt to divide by zero results in an error with exit code 4: Integer overflow
.
It can only be applied to values of type Int
:
let two: Int = 2;two / 2; // 1two / 1; // 2-1 / 5; // -1-1 / -5; // 01 / -5; // -11 / 5; // 06 / 5; // 1, rounding down-6 / 5; // -2, rounding down (toward -∞)
Modulo, %
The binary percent sign (modulo) operator %
is used for obtaining the modulo of integer division, which must not be confused with obtaining the remainder. For two values with the same sign, modulo and remainder operations are equivalent, but when the operands have different signs, the modulo result always has the same sign as the divisor (the value on the right), while the remainder has the same sign as the dividend (the value on the left). This difference can cause the results to differ by one unit of the divisor.
It can only be applied to values of type Int
:
let two: Int = 2;two % 2; // 0two % 1; // 0
1 % 5; // 1-1 % 5; // 41 % -5; // -4-1 % -5; // -1
The simplest way to avoid confusion between obtaining the modulo and obtaining the remainder is to use only unsigned integers. Alternatively, consider using the abs()
function to ensure non-negative values:
abs(-1) % abs(-5); // 1
Addition
Add or subtract.
Add, +
The binary plus (addition) operator +
is used for adding numbers together. Going beyond the maximum value of an Int
will result in an error with exit code 4: Integer overflow
.
It can only be applied to values of type Int
:
let two: Int = 2;two + 2; // 4-1 + 1; // 0
pow(2, 254) + pow(2, 254); // 2 * 2^{254}pow(2, 255) + pow(2, 255); // build error: integer overflow!pow(2, 255) - 1 + pow(2, 255); // 2^{256} - 1, maximal value of any integer in Tact!
Subtract, -
The binary minus (subtraction) operator -
is used for subtracting numbers from each other. Going beyond the minimum value of an Int
will result in an error with exit code 4: Integer overflow
.
It can only be applied to values of type Int
:
let two: Int = 2;two - 2; // 0-1 - 1; // -2
pow(2, 254) - pow(2, 254); // 0pow(2, 255) - pow(2, 255); // 0pow(2, 256) - pow(2, 256); // build error: integer overflow!
Bitwise shifts
Shift bits to the left or to the right.
Shift right, >>
The binary double greater than (bitwise shift right) operator >>
returns an integer whose binary representation is the left operand value shifted by the right operand number of bits to the right. Excess bits shifted off to the right are discarded, and copies of the leftmost bit are shifted in from the left. This operation is also called “sign-propagating right shift” or “arithmetic right shift” because the sign of the resulting number is the same as the sign of the left operand. This is a more efficient way to divide the left operand by , where is equal to the right operand.
Can only be applied to values of type Int
:
let two: Int = 2;two >> 1; // 14 >> 1; // 25 >> 1; // 2, due to flooring of integer values
pow(2, 254) >> 254; // 1
Shift left, <<
The binary double less-than (bitwise shift left) operator <<
returns an integer whose binary representation is the left operand value shifted to the left by the number of bits specified by the right operand. Excess bits shifted off from the left are discarded, and zero bits are shifted in from the right. This is a more efficient way to multiply the left operand by , where is equal to the right operand. Exceeding the maximum value of an Int
will result in an error with exit code 4: Integer overflow
.
Can only be applied to values of type Int
:
let two: Int = 2;two << 1; // 41 << 5; // 1 * 2^5, which is 322 << 5; // 2 * 2^5, which is 64
pow(2, 254) == (1 << 254); // truepow(2, 254) == 1 << 254; // true, no parentheses needed due to higher precedence of << over ==pow(2, 255) == 1 << 255; // true, but we're very close to overflow here!
Relation
Find greater, smaller, or equal values.
Greater than, >
The binary greater than operator >
returns true
if the left operand is greater than the right operand and false
otherwise. Can only be applied to values of type Int
:
let two: Int = 2;two > 2; // false-1 > -3; // true
Greater than or equal to, >=
The binary greater than or equal to operator >=
returns true
if the left operand is greater than or equal to the right operand and false
otherwise. Can only be applied to values of type Int
:
let two: Int = 2;two >= 2; // true-1 >= -3; // true
Less than, <
The binary less than operator <
returns true
if the left operand is less than the right operand, and false
otherwise. It can only be applied to values of type Int
:
let two: Int = 2;two < 2; // false-1 < -3; // false
Less than or equal to, <=
The binary less than or equal to operator <=
returns true
if the left operand is less than or equal to the right operand, and false
otherwise. It can only be applied to values of type Int
:
let two: Int = 2;two <= 2; // true-1 <= -3; // false
Equality and inequality, ==
!=
The binary equality (equal) operator ==
checks whether its two operands are equal, returning a result of type Bool
.
The binary inequality (not equal) operator !=
checks whether its two operands are not equal, returning a result of type Bool
.
Both operators require operands to be of the same type, and neither operator performs implicit type conversions, except for the Cell
and Slice
types, which are implicitly compared by their hashes.
Both operators can be applied to the following list of types and values:
Int
Bool
Address
Cell
, implicitly compared via.hash()
Slice
, implicitly compared via.hash()
String
map<K, V>
, but only if their key and value types are identical- Optionals and
null
value
// Int:2 == 3; // false2 != 3; // true
// Bool:true == true; // truefalse != true; // true
// Address:myAddress() == myAddress(); // truemyAddress() != myAddress(); // false
// Cell:emptyCell() == emptyCell(); // trueemptyCell() != emptyCell(); // false
// Slice:"A".asSlice() == "A".asSlice(); // true"A".asSlice() != "A".asSlice(); // false
// String:"A" == "A"; // true"A" != "A"; // false
// map<K, V>:let map1: map<Int, Int> = emptyMap();let map2: map<Int, Int> = emptyMap();map1 == map2; // truemap1 != map2; // false
// Optionals and null values themselveslet nullable: Int? = null;nullable == null; // truenull == null; // truenullable != null; // falsenull != null; // false
let anotherNullable: Int? = 5;nullable == anotherNullable; // falsenullable != anotherNullable; // true
Bitwise AND, &
The binary ampersand (bitwise AND) operator &
applies a bitwise AND, which performs the logical AND operation on each pair of corresponding bits of the operands. This is useful when we want to clear selected bits of a number, where each bit represents an individual flag or a boolean state. This makes it possible to “store” up to 257 boolean values per integer, as all integers in Tact are 257-bit signed.
It can only be applied to values of type Int
:
let two: Int = 2;two & 1; // 04 & 1; // 03 & 1; // 11 & 1; // 1
255 & 0b00001111; // 150b11111111 & 0b00001111; // 15
Bitwise XOR, ^
The binary caret (bitwise XOR) operator ^
applies a bitwise XOR, performing the logical exclusive OR operation on each pair of corresponding bits of the operands. The result in each position is if exactly one of the bits is , or if both bits are or both bits are . Thus, it compares two bits, yielding if the bits are different and if they are the same.
It is useful for inverting selected bits of an operand (also called toggling or flipping), as any bit can be toggled by “XORing” it with .
It can only be applied to values of type Int
:
let two: Int = 2;two ^ 3; // 14 ^ 1; // 53 ^ 1; // 21 ^ 1; // 0
255 ^ 0b00001111; // 2400b11111111 ^ 0b00001111; // 240
Bitwise OR, |
The binary bar (bitwise OR) operator |
applies a bitwise OR, which performs the logical OR operation on each pair of corresponding bits of the operands. This is useful when we want to apply a specific bitmask.
For example, bitwise OR is commonly used in Tact to combine base modes with optional flags by masking specific bits to in order to construct a target message mode
.
Can only be applied to values of type Int
:
let two: Int = 2;two | 1; // 34 | 1; // 53 | 1; // 31 | 1; // 1
255 | 0b00001111; // 2550b11111111 | 0b00001111; // 255
Logical AND, &&
The binary logical AND (logical conjunction) operator &&
returns true
if both operands are true
and false
otherwise. It’s short-circuited, meaning that it immediately evaluates the entire expression as false
if the left operand is false
, without evaluating the right one.
Can only be applied to values of type Bool
:
let iLikeTact: Bool = true;iLikeTact && true; // true, evaluated both operandsiLikeTact && false; // false, evaluated both operandsfalse && iLikeTact; // false, didn't evaluate iLikeTact
Logical OR, ||
The binary logical OR (logical disjunction) operator ||
returns false
only if both operands are false
, and true
otherwise. It is short-circuited, meaning that it immediately evaluates the whole expression as true
if the left operand is true
, without evaluating the right one.
This operator can only be applied to values of type Bool
:
let iLikeSnails: Bool = false;iLikeSnails || true; // true, evaluated both operandsiLikeSnails || false; // false, evaluated both operandstrue || iLikeSnails; // true, didn't evaluate iLikeSnails
Ternary, ?:
The conditional (ternary) operator is the only Tact operator that takes three operands: a condition followed by a question mark (?
), then an expression to execute if the condition evaluates to true
, followed by a colon (:
), and finally the expression to execute if the condition evaluates to false
. This operator is frequently used as an alternative to an if...else
statement.
The condition must resolve to type Bool
:
// condition// ↓true ? "incredibly so" : "absolutely not"; // "incredibly so"// --------------- ----------------// ↑ ↑// | alternative, when condition is false// |// consequence, when condition is true
2 + 2 == 4 ? true : false; // true
The ternary operator is the only operator with right associativity, besides assignment-related ones. This means that in ambiguous situations, Tact will prefer the longest matching sequence. In short, this makes bracket-less nesting of ternary operators possible, but only for alternative cases (the part that comes after the colon sign :
):
// don't need additional parentheses for alternative casesfalse ? 1 : (false ? 2 : 3); // 3false ? 1 : false ? 2 : 3; // also 3false ? 1 : true ? 2 : 3; // 2
// need additional parentheses for consequence cases (parts between ? and :)false ? (false ? 1 : 2) : 3; // 3false ? false ? 1 : 2 : 3; // SYNTAX ERROR!true ? (false ? 1 : 2) : 3; // 2
Assignment, =
The assignment operator =
is used to assign a value to a variable or to a property of a Message or a Struct. The assignment is a statement, and it does not return a value.
let someVar: Int = 5; // assignment operator = is used here...someVar = 4; // ...and heresomeVar = (someVar = 5); // SYNTAX ERROR!
Augmented assignment
Augmented (or compound) assignment operators such as +=
combine an operation with an assignment. An augmented assignment is a statement and does not return a value.
Augmented assignments are semantically equivalent to regular assignments but include an operation:
let value: Int = 5;
// this:value += 5;// is equivalent to this:value = value + 5;
List of augmented assignment operators:
+=
, which uses the addition operator+
. Can only be applied to values of typeInt
.-=
, which uses the subtraction operator-
. Can only be applied to values of typeInt
.*=
, which uses the multiplication operator*
. Can only be applied to values of typeInt
./=
, which uses the division operator/
. Can only be applied to values of typeInt
.%=
, which uses the modulo operator%
. Can only be applied to values of typeInt
.&=
, which uses the bitwise AND operator&
. Can only be applied to values of typeInt
.^=
, which uses the bitwise XOR operator^
. Can only be applied to values of typeInt
.|=
, which uses the bitwise OR operator|
. Can only be applied to values of typeInt
.&&=
, which uses the logical AND operator&&
. Can only be applied to values of typeBool
. Available since Tact 1.6.||=
, which uses the logical OR operator||
. Can only be applied to values of typeBool
. Available since Tact 1.6.<<=
, which uses the bitwise shift left operator<<
. Can only be applied to values of typeInt
. Available since Tact 1.6.>>=
, which uses the bitwise shift right operator>>
. Can only be applied to values of typeInt
. Available since Tact 1.6.
let value: Int = 5;
// +=value + 5; // adds 5value = value + 5; // adds 5 and assigns result backvalue += 5; // also adds 5 and assigns result back
// -=value - 5; // subtracts 5value = value - 5; // subtracts 5 and assigns result backvalue -= 5; // also subtracts 5 and assigns result back
// *=value * 5; // multiplies by 5value = value * 5; // multiplies by 5 and assigns result backvalue *= 5; // also multiplies by 5 and assigns result back
// /=value / 5; // divides by 5value = value / 5; // divides by 5 and assigns result backvalue /= 5; // also divides by 5 and assigns result back
// %=value % 5; // gets modulo by 5value = value % 5; // gets modulo by 5 and assigns result backvalue %= 5; // also gets modulo by 5 and assigns result back
// &=value & 5; // bitwise ANDs with 5value = value & 5; // bitwise ANDs with 5 and assigns result backvalue &= 5; // also bitwise ANDs with 5 and assigns result back
// ^=value ^ 5; // bitwise XORs with 5value = value ^ 5; // bitwise XORs with 5 and assigns result backvalue ^= 5; // also bitwise XORs with 5 and assigns result back
// |=value | 5; // bitwise ORs with 5value = value | 5; // bitwise ORs with 5 and assigns result backvalue |= 5; // also bitwise ORs with 5 and assigns result back
//// The following augmented assignment operators are available since Tact 1.6//
// <<=value << 5; // bitwise shifts left by 5value = value << 5; // bitwise shifts left by 5 and assigns result backvalue <<= 5; // also bitwise shifts left by 5 and assigns result back
// >>=value >> 5; // bitwise shifts right by 5value = value >> 5; // bitwise shifts right by 5 and assigns result backvalue >>= 5; // also bitwise shifts right by 5 and assigns result back
let bValue: Bool = true;
// &&=bValue && false; // logically ANDs with falsebValue = bValue && false; // logically ANDs with false and assigns result backbValue &&= false; // also logically ANDs with false and assigns result back
// ||=bValue || true; // logically ORs with truebValue = bValue || true; // logically ORs with true and assigns result backbValue ||= true; // also logically ORs with true and assigns result back