Operators
Arithmetic
| Operator | What it does | Example |
|---|---|---|
+ - * | addition, subtraction, multiplication | 2 + 3 * 4 → 14 |
/ | division; int / int is integer | 7 / 2 → 3, 7.0 / 2 → 3.5 |
% | remainder, sign of the divisor | -7 % 3 → 2 |
** | power, right-associative | 2 ** 10 → 1024 |
-x | negation | -(2 ** 2) → -4 |
mzs
7 / 2 # → 3
-7 / 2 # → -3
-7 % 3 # → 2
2 ** 3 ** 2 # → 512
2 ** -1 # → 0.5
1 / 0.0 # → InfinityInteger division by zero is a zero-division error. -2 ** 2 does not compile: write -(2 ** 2) or (-2) ** 2.
Strings, arrays, dicts
| Operator | What it does | Example |
|---|---|---|
+ | concatenates strings and arrays, merges dicts | [1, 2] + [3] → [1,2,3] |
* | repeats a string or an array | "ab" * 3 → "ababab" |
- | array difference, order kept | [1, 2, 3] - [2] → [1,3] |
% | string formatting | "%d pcs" % 5 → "5 pcs" |
mzs
"a" + "b" # → "ab"
[1, 2] * 2 # → [1,2,1,2]
[1, 2, 3] - [2, 3, 9] # → [1]
{a: 1} + {a: 2, b: 3} # → {"a":2,"b":3}
"%s: %d" % ["total", 42] # → "total: 42"No implicit conversions
mzs
"2" + 1text
type: cannot add int to stringmzs
"2".int + 1 # → 3
"2" + 1.str # → "21"The only implicit conversion is interpolation, "${x}".
Comparison
| Operator | What it does | Example |
|---|---|---|
== != | equality by value, between any types | 1 == 1.0 → true |
< <= > >= | order of numbers, strings, arrays | "a" < "b" → true |
<=> | -1, 0, 1 or nil | 5 <=> 3 → 1 |
~ !~ | regular expression match | "abc" ~ /b/ → true |
in | is there such an element, key or substring | 2 in [1, 2] → true |
mzs
[1] <=> [1, 2] # → -1
"hello" ~ /llo/ # → true
5 in 1..20 # → true
20 in 1..<20 # → false
"k" in {k: 1} # → true
"ell" in "hello" # → true
!(4 in [1, 2]) # → trueComparisons do not chain: 1 < x < 3 is an error, write 1 < x && x < 3. There is no not in.
Logic and nil
| Operator | What it does | Example |
|---|---|---|
&& | the left one if it is falsy, else the right one | 0 && "x" → "x" |
|| | the left one if it is truthy, else the right one | nil || "x" → "x" |
! | negation, always a bool | !nil → true |
?? | the right one only if the left one is nil | false ?? 1 → false |
?. | stops the chain at nil | nil?.upper → nil |
? : | ternary | x > 0 ? "+" : "-" |
mzs
false || "fallback" # → "fallback"
nil ?? "default" # → "default"
false ?? "default" # → false
name = nil
name?.upper.len # → nil
3 > 2 ? "yes" : "no" # → "yes"Ranges
a..b includes b, a..<b does not.
mzs
(1..5).array # → [1,2,3,4,5]
(1..<5).array # → [1,2,3,4]
(5..1).array # → []
(1..5).len # → 5
(0..4).map { it * it } # → [0,1,4,9,16]0..5.map { … } is an error: a method on a range is called in parentheses, (0..5).map.
Assignment
| Operator | What it does |
|---|---|
= | writes to the nearest existing variable or creates a new one |
:= | always creates a new one in the current scope |
+= -= *= /= %= **= | x = x op y |
||= | assign if falsy |
&&= | assign if truthy |
??= | assign if nil |
mzs
z = [1, 2]
z[0] += 10
z # → [11,2]
a = nil
a ??= 5
a # → 5
c = false
c ||= 9
c # → 9Bitwise operations
&, |, ^ are not operators. There are functions:
mzs
[band(6, 3), bor(6, 3), bxor(6, 3), bnot(0)] # → [2,7,5,-1]
[shl(1, 4), shr(16, 2)] # → [16,4]Precedence
From tightest to loosest:
| Level | Operators |
|---|---|
| 1 | x.y x?.y x(…) x[…] |
| 2 | ** |
| 3 | ! - (unary) |
| 4 | * / % |
| 5 | + - |
| 6 | .. ..< |
| 7 | in |
| 8 | < <= > >= <=> |
| 9 | == != ~ !~ |
| 10 | && |
| 11 | || |
| 12 | ?? |
| 13 | ? :, try … else … |
| 14 | = and compound assignments |
| 15 | if, while modifiers |
mzs
2 + 3 * 4 # → 14
!true == false # → true
1 + 2..5 # → 3..5