Operators
Arithmetic operators
The following table shows which operators are implemented for which types.
| Left type | Operator | Right type | Example |
|---|---|---|---|
| number | + | number | 1 + 2 == 3 |
| string | + | any | "text" + 3 == "text3" |
| array | + | array | [1, 2] + [3, 4] == [1, 2, 3, 4] |
| array | + | any | [1, 2] + 3 == [1, 2, 3] |
| number | - | number | 3 - 4 == -1 |
| number | * | number | 3 * -4 == -12 |
| number | / | number | 2 / 4 == 0.5 |
| number | % | number | 7 % 3 == 1 |
All other operator/type combinations raise an error and cancel the order.
Furthermore, if the right value of / and % is 0, an error is raised and the order gets canceled.
Logical operators
As logical operators and and or can be used. For that, the left and the right operand are converted to their truth values and the operators are evaluated as follows.
| Left | Operator | Right | Result |
|---|---|---|---|
| false | and | false | false |
| false | and | true | false |
| true | and | false | false |
| true | and | true | true |
| false | or | false | false |
| false | or | true | true |
| true | or | false | true |
| true | or | true | true |
Logical operators get short-circuited evaluated, which means if the operator is and and the left value is false, the right expression is not evaluated at all. Vice versa if the operator is or and the left value is true, the right expression is not evaluated.
That means that the following expression evaluates to true without canceling the order: true or order::cancel().
Relational operators
To compare values, two equality operators and four comparison operators are provided by the language.
Equality
| Expression | Meaning |
|---|---|
a == b | a is equal to b |
a != b | a is not equal to b |
a and b can be of any type, but the operators are type strict, which means that 2 == "2" is false. Arrays and objects are compared recursively.
Comparison
| Expression | Meaning |
|---|---|
a < b | a is less than b |
a <= b | a is less than or equal to b |
a > b | a is greater than b |
a > b | a is greater than or equal to b |
Comparison operators should only be used for numbers. Because if either a or b is not a number, the value of the expression is always false.
Unary operators
| Operator | Type | Example |
|---|---|---|
- | number | -3 == -3 |
! | any | !{ k: "v" } == false |
All other operator/type combinations raise an error and cancel the order.
Null coalescing operator
To set a default value if a value is null, the null coalescing operator ?? can be used. The default value can be of any type.
Example:
1 ?? 2 == 3
null ?? "default" == "default"
Precedence
- Logical:
and, andor - Relational:
==,!=,<,<=,>, and>= - Additive:
+, and- - Multiplicative:
*,/, and% - Unary:
-, and!,<- - Null coalescing:
?? - Offset, Member, Call:
[],., and() - Values and variables:
23,"test",var, …
Except unary operators, multiple operators on the same precedence level are evaluated left-associative. Unary operators are evaluated right-associative.
Example:
1 + 2 + 3 == ((1 + 2) + 3)
1 + 2 * 3 == (1 + (2 * 3))
1 + 2 == 3 or -4 >= 6 == (((1 + 2) == 3) or ((-4) <= 6))
-4 ?? "default" == (-(4 ?? "default"))
test()[2].key == ((((test)())[2]).key)
-<-var ?? 5 == (-(<-(var ?? 5)))