Operators are special functions defined in the Verse programming language to perform actions such as math operations on their operands. For example, in the expression 1 + 2
, the +
is an operator, and 1
and 2
are both operands.
There are three formats for operators that you’ll see in Verse:
Prefix: There is only one operand and the operator is before the operand.
Infix: There are two operands and the operator is between the operands.
Postfix: There is only one operand and the operator is after the operand.
This page describes all the operators you can use in Verse, how they work, and their order of evaluation when used in combination with other operators.
List of All Operators and Operator Precedence
When multiple operators are used in the same expression, they are evaluated in the order of highest to lowest precedence. The table below lists all built-in operators in Verse and their precedence.
Name | Operator | Description | Operator Format | Operator Precedence | Example |
---|---|---|---|---|---|
Query |
| The | Postfix | 9 |
|
Not |
| The | Prefix | 8 |
|
Positive |
| You can use the | Prefix | 8 |
|
Negative |
| You can use the operator | Prefix | 8 |
|
Multiplication |
| The | Infix | 7 |
|
Division |
| The | Infix | 7 |
|
Addition |
| The | Infix | 6 |
|
Subtraction |
| The | Infix | 6 |
|
Addition assignment |
| With this operator, you can combine addition and assignment in the same operation to update a variable's value. See Math for more details. | Infix | 5 |
|
Subtraction assignment |
| With this operator, you can combine subtraction and assignment in the same operation to update a variable's value. See Math for more details. | Infix | 5 |
|
Multiplication assignment |
| With this operator, you can combine multiplication and assignment in the same operation to update a variable's value. See Math for more details. | Infix | 5 |
|
Division assignment |
| With this operator, you can combine division and assignment in the same operation to update a variable's value, unless the variable is an integer. See Math for more details. | Infix | 5 |
|
Equal to |
| The | Infix | 4 |
|
Not equal to |
| The | Infix | 4 |
|
Less than |
| The | Infix | 4 |
|
Less than or equal to |
| The | Infix | 4 |
|
Greater than |
| The | Infix | 4 |
|
Greater than or equal to |
| The | Infix | 4 |
|
And |
| The | Infix | 3 |
|
Or |
| The | Infix | 2 |
|
Variable and constant initialization |
| With this operator, you can store values in a constant or variable. See Constants and Variables for more details. | Infix | 1 |
|
Variable assignment |
| With this operator, you can update the values stored in a variable. See Constants and Variables for more details. | Infix | 1 |
|
If there are operators with the same precedence in the same expression, then they are evaluated left to right. For example in the expression 3*2/4
, both operators *
and /
have the same precedence, so 3*2
is evaluated first and its result becomes the left operand for the /
operator.
You can change the order in which operators are evaluated by grouping expressions with ()
. For example, (1+2)*3
and 1+(2*3)
don't evaluate to the same result. See Grouping for more details.
Comparison
You can control the success and failure flow with comparison expressions, which use the inequality and equality operators. Comparison expressions are failable, so you can only use comparison operators in failure contexts, such as in if expressions.
The table below describes each operator and what types it supports. All comparison operators use the infix format.
Operator | Supported BUilt-In Types | Description |
---|---|---|
|
| The |
|
| The |
|
| The |
|
| The |
|
| The |
|
| The |
Both <>
and =
are also supported for array
, map
, tuple
, and class
instances, but with restrictions. The array
, map
, and tuple
instances can only contain supported types, and class
instances are only supported if they contain at least one var
member.
Decision
You can control the success and failure flow with decision expressions, which use the operators not
, and
, and or
. Decision expressions are failable, so you can only use comparison operators in failure contexts, such as in if
expressions. You can use any expressions that succeed or fail with decision operators.
Not Operator
The decision operator not
negates the success or failure of an expression. The not
operator uses the prefix format.
For example, when expression
fails, not expression
will succeed. When expression
succeeds, not expression
will fail and the effects of expression
are never committed (as if the expression never happened).
For example, after the following code is executed, Example
will still have the initial value 0
:
var Example : int = 0
if (not (set Example = ExampleArray[0])) { … }
You can use not not expression
as a way to check if an expression will succeed but make it so the expression never happens.
Outcome of the Expression p | Outcome of the Expression not p | Outcome of the Expression not not p |
---|---|---|
Succeeds and the result is | The expression fails, and the effects of | The expression succeeds, but the effects of |
Fails and the result is no value | The expression succeeds. The result of the expression is | The expression fails. The result of the expression is no value. |
Reference for the not operator evaluating an expression, represented by p.
And / Or Operators
The decision operator and
uses the infix format and is a failable expression that succeeds if both operands succeed, or fails if at least one operand fails.
The decision operator or
uses the infix format and is:
A failure context for the first operand.
A failable expression only if the second operand is failable.
The or
operator skips evaluation of the second operand if the first operand succeeds.
The table below describes the results of all the operand combinations of success and failure for decision expressions using the operators and
and or
.
Outcome of the Expression p | Outcome of the Expression q | Outcome of the Expression p and q | Outcome of the Expression p or q |
---|---|---|---|
Succeeds and result is | Succeeds and the result is | The expression succeeds, so the effects of both | The expression succeeds, and only the effects of |
Succeeds and the result is | Fails and the result is no value | The expression fails, and the effects of both | The expression succeeds, and only the effects of |
Fails and the result is no value | Succeeds and the result is | The expression fails, and the effects of both | The expression succeeds, and only the effects of |
Fails and the result is no value | Fails and the result is no value | The expression fails, and the effects of both | The expression fails, and the effects of both |
Reference for the and and or operators evaluating expressions, represented by p and q.
Math
With math expressions, you can do the four basic math operations (addition, subtraction, multiplication, and division) with number values, and add strings together. All the operators use the infix format, except +
and -
can also be a prefix for number values.
There are also assign operators, e.g., set X += 10
. They are almost the same as doing the operation and then assigning the result, set X = X + 10
, the difference is that the X
in this case is only evaluated once. The result of an assignment operator is the value used to update the variable.
The table below describes each operator and what types it supports.
Operator | Supported Built-In Types | Description |
---|---|---|
|
| The |
|
| The |
|
| The |
|
| The |
|
| With this operator, you can combine addition and assignment in the same operation to update a variable's value. |
|
| With this operator, you can combine subtraction and assignment in the same operation to update a variable's value. |
|
| With this operator, you can combine multiplication and assignment in the same operation to update a variable's value. |
|
| With this operator, you can combine division and assignment in the same operation to update a variable's value, unless the variable is an integer. For more details on integer division, see int. |
Query
Query expressions use the operator ?
(query) and check if a logic
value is true
. Otherwise, the expression fails. The ?
(query) operator uses the postfix format.
Outcome of the Expression p | Outcome of the Expression p? |
---|---|
| Succeeds and result is |
| Fails and result is no value. |
Reference for the ? (query) operator evaluating expressions, represented by p.
For example:
if (IsMorning?):
Say("Good Morning!")