Skip to content

Operators ​

Arithmetic ​

OperatorWhat it doesExample
+ - *addition, subtraction, multiplication2 + 3 * 4 → 14
/division; int / int is integer7 / 2 → 3, 7.0 / 2 → 3.5
%remainder, sign of the divisor-7 % 3 → 2
**power, right-associative2 ** 10 → 1024
-xnegation-(2 ** 2) → -4
mzs
7 / 2  # → 3
-7 / 2  # → -3
-7 % 3  # → 2
2 ** 3 ** 2  # → 512
2 ** -1  # → 0.5
1 / 0.0  # → Infinity

Integer division by zero is a zero-division error. -2 ** 2 does not compile: write -(2 ** 2) or (-2) ** 2.

Strings, arrays, dicts ​

OperatorWhat it doesExample
+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" + 1
text
type: cannot add int to string
mzs
"2".int + 1  # → 3
"2" + 1.str  # → "21"

The only implicit conversion is interpolation, "${x}".

Comparison ​

OperatorWhat it doesExample
== !=equality by value, between any types1 == 1.0 → true
< <= > >=order of numbers, strings, arrays"a" < "b" → true
<=>-1, 0, 1 or nil5 <=> 3 → 1
~ !~regular expression match"abc" ~ /b/ → true
inis there such an element, key or substring2 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])  # → true

Comparisons do not chain: 1 < x < 3 is an error, write 1 < x && x < 3. There is no not in.

Logic and nil ​

OperatorWhat it doesExample
&&the left one if it is falsy, else the right one0 && "x" → "x"
||the left one if it is truthy, else the right onenil || "x" → "x"
!negation, always a bool!nil → true
??the right one only if the left one is nilfalse ?? 1 → false
?.stops the chain at nilnil?.upper → nil
? :ternaryx > 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 ​

OperatorWhat 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  # → 9

Bitwise 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:

LevelOperators
1x.y x?.y x(…) x[…]
2**
3! - (unary)
4* / %
5+ -
6.. ..<
7in
8< <= > >= <=>
9== != ~ !~
10&&
11||
12??
13? :, try … else …
14= and compound assignments
15if, while modifiers
mzs
2 + 3 * 4  # → 14
!true == false  # → true
1 + 2..5  # → 3..5