Operators
Almost every contract operates on data: transforms some values into another. Scope may vary, but operators lay in 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 | >> << |
Relation | > >= < <= |
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 choose which operator would be considered in a particular situation. Whenever any ambiguity arises, Tact would prefer operators with higher precedence over those with lower.
For example, minus sign (-
) may be considered as a subtraction operator or as a negation operator, which reverses the sign of the expression from plus to minus, or vice-versa. As the latter has the higher precedence over the former in cases of ambiguity between the two Tact will first consider -
as a negation operator. And if that doesn’t make sense for the given expression, only then it would consider it as a subtraction operator.
Consider the following code:
Even though this example may be simple, neglecting of 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 there is.
Parentheses, ()
Parentheses (also can be called round brackets, ()
) are more of a punctuation symbols than actual operators, but their precedence is higher than precedence of any other operator. Use parentheses to override order of operations:
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, are of the same precedence.
Unary operators can be one of the two types:
- Prefix — placed before the expression.
- Postfix (or suffix) — placed after the expression.
Non-null assert, !!
Unary double-exclamation mark (non-null assertion) operator !!
is a postfix operator, which enforces non-null
values and allows direct access to the value of the optional variable if it’s not null
. Otherwise, raises a compilation error if the compiler can track it, and if not — throws an exception with exit code 128: Null reference exception
. Can be applied to any optional variable regardless of its non-null
type.
Plus, +
Although unary plus sign operator +
is specified in the grammar of Tact compiler, it only exists as a binary operator.
Negate, -
Unary minus sign (negation) operator -
is a prefix operator, which reverses the sign of the expression. Can only be applied to values of type Int
:
Inverse, !
Unary exclamation mark (inversion) operator !
is a prefix operator, which inverts the boolean value of the expression — changes true
to false
, and vice versa. Can only be applied to values of type Bool
:
Bitwise NOT, ~
Unary tilde (bitwise not) operator ~
is a prefix operator, which inverts or flips each bit in the binary representation of the expression — changes each to , and vice versa. Can only be applied to values of type Int
:
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, *
Binary asterisk (multiplication) operator *
is used for multiplication of two values. Can cause integer overflows.
Can only be applied to values of type Int
:
Divide, /
Binary slash (division) operator /
is used for integer division of two values, which truncates towards zero if result is positive, and away from zero if result is negative. This is also called rounding down (or rounding towards ).
An attempt to divide by zero would result in an error with exit code 4: Integer overflow
.
Can only be applied to values of type Int
:
Modulo, %
Binary percent sign (modulo) operator %
is used for getting the modulo of an integer division, which must not be confused with getting a remainder. For two values of the same sign, modulo and remainder operations are equivalent, but when the operands are of different signs, the modulo result always has the same sign as the divisor (value on the right), while the remainder has the same sign as the dividend (value on the left), which can make them differ by one unit of the divisor.
Can only be applied to values of type Int
:
The simplest way to avoid confusion between the two is to prefer using positive values via abs(x: Int)
:
Addition
Add or subtract.
Add, +
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
.
Can only be applied to values of type Int
:
Subtract, -
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
.
Can only be applied to values of type Int
:
Bitwise shifts
Shift bits to the left or to the right.
Shift right, >>
Binary double greater than (bitwise shift right) operator >>
returns an integer which 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 effective way to divide the left operand by , where is equal to the right operand.
Can only be applied to values of type Int
:
Shift left, <<
Binary double greater than (bitwise shift left) operator <<
returns an integer which binary representation is the left operand value shifted by the right operand number of bits to the left. Excess bits shifted off to the left are discarded, and zero bits are shifted in from the right. This is a more effective way to multiply the left operand by , where is equal to the right operand. Going beyond 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
:
Relation
Find bigger, smaller or equal values.
Greater than, >
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
:
Greater than or equal to, >=
Binary greater than or equal to operator >=
returns true
if the left operand is greater than or to the right operand, and false
otherwise. Can only be applied to values of type Int
:
Less than, <
Binary less than operator <
returns true
if the left operand is less than the right operand, and false
otherwise. Can only be applied to values of type Int
:
Less than or equal to, <=
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. Can only be applied to values of type Int
:
Equality and inequality, ==
!=
Binary equality (equal) operator ==
checks whether its two operands are equal, returning a result of type Bool
.
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 both don’t perform 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 compares via.hash()
Slice
, implicitly compares via.hash()
String
map<K, V>
, but only if their key and value types are identical- Optionals and
null
value
Bitwise AND, &
Binary ampersand (bitwise AND) operator &
applies a bitwise AND, which performs the logical AND operation on each pair of the corresponding bits of operands. This is useful when we want to clear selected bits off a number, where each bit represents an individual flag or a boolean state, which makes it possible to “store” up to boolean values per integer, as all integers in Tact are -bit signed.
Can only be applied to values of type Int
:
Bitwise XOR, ^
Binary caret (bitwise XOR) operator ^
applies a bitwise XOR, which performs the logical exclusive OR operation on each pair of the corresponding bits of operands. The result in each position is if only one of the bits is , but will be if both are or both are . In this it performs the comparison of two bits, giving if the two bits are different, and if they are the same.
It is useful for inverting selected bits of an operand (also called toggle or flip), as any bit may be toggled by “XORing” it with .
Can only be applied to values of type Int
:
Bitwise OR, |
Binary bar (bitwise OR) operator |
applies a bitwise OR, which performs the logical OR operation on each pair of the corresponding bits of 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
:
Logical AND, &&
Binary logical AND (logical conjunction) operator &&
returns true
if both operands are true
, and false
otherwise. It’s short-circuited, meaning that it would immediately evaluate the whole expression as false
if the left operand is false
, without evaluating the right one.
Can only be applied to values of type Bool
:
Logical OR, ||
Binary logical OR (logical disjunction) operator ||
returns false
only if both operands are false
, and true
otherwise. It’s short-circuited, meaning that it would immediately evaluate the whole expression as true
if the left operand is true
, without evaluating the right one.
Can only be applied to values of type Bool
:
Ternary, ?:
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 is evaluated to true
followed by a colon (:
), and finally the expression to execute if the condition is evaluated to false
. This operator is frequently used as an alternative to an if...else
statement.
Condition must resolve to type Bool
:
Ternary operator is the only operator with right associativity, besides assignment-related ones. This means that in ambiguous situations Tact would 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 :
):
Assignment, =
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 doesn’t return a value.
Augmented assignment
Augmented (or compound) assignment operators such as +=
combine an operation with an assignment. The augmented assignment is a statement and it doesn’t return a value.
Augmented assignments are semantically equivalent to regular assignments, but with an operation:
List of augmented assignment operators:
+=
, which uses addition operator+
. Can only be applied to values of typeInt
.-=
, which uses subtraction operator-
. Can only be applied to values of typeInt
.*=
, which uses multiplication operator*
. Can only be applied to values of typeInt
./=
, which uses division operator/
. Can only be applied to values of typeInt
.%=
, which uses modulo operator%
. Can only be applied to values of typeInt
.&=
, which uses bitwise AND operator&
. Can only be applied to values of typeInt
.^=
, which uses bitwise XOR operator^
. Can only be applied to values of typeInt
.|=
, which uses bitwise OR operator|
. Can only be applied to values of typeInt
.