Operators
Operators are listed from lowest to highest precedence. Unless noted otherwise, binary operators are left-associative.
| Level | Operators | Associativity | Notes |
|---|---|---|---|
| 0 | =, +=, -=, *=, /=, &=, |=, ^=, <<=, >>= |
Right | Assignment |
| 1 | or |
Left | Short-circuit |
| 2 | and |
Left | Short-circuit |
| 3 | ==, != |
Left | Equality |
| 4 | <, <=, >, >= |
Left | Comparison |
| 4.5 | .. |
Left | Range literal |
| 4.6 | | |
Left | Bitwise or |
| 4.7 | ^ |
Left | Bitwise xor |
| 4.8 | & |
Left | Bitwise and |
| 4.9 | <<, >> |
Left | Shifts |
| 5 | +, - |
Left | Addition, subtraction |
| 6 | *, /, % |
Left | Multiplication, division, modulo |
| 7 | not, -, +, ~ |
Right | Unary |
| 8 | f(a, b), o.m(), o.f, a[i] |
Left | Call and access |
Arithmetic
Section titled “Arithmetic”print(2 + 3) # 5print(7 - 2) # 5print(4 * 5) # 20print(17 / 2) # 8, integer divisionprint(7.0 / 2) # 3.5print(17 % 5) # 2/ on two ints truncates toward zero. If either operand is a float, the result is a float. Division and modulo by zero raise RuntimeError for both ints and floats. There is no inf or nan.
Integer arithmetic wraps on overflow:
print(9223372036854775807 + 1) # -9223372036854775808+ also concatenates strings. There is no ** exponentiation; use math.pow. There is no // integer division operator.
Unary operators
Section titled “Unary operators”print(-5)print(not true) # falseprint(~5) # -6! is not a logical NOT operator. Use not.
Bitwise operators
Section titled “Bitwise operators”Bitwise operators require integer operands. Floats, bools, strings, and none raise TypeError.
print(5 & 3) # 1print(5 | 3) # 7print(5 ^ 3) # 6print(~5) # -6print(1 << 4) # 16print(-4 >> 1) # -2>> sign-extends. Shift counts must be non-negative. Counts of 64 or more do not raise: x << 64 is 0, and x >> 64 is 0 for non-negative x and -1 for negative x.
Bitwise precedence is Python-style: shifts bind tighter than &, which binds tighter than ^, which binds tighter than |, and all of them bind tighter than comparisons.
print(5 & 3 == 1) # true, parsed as (5 & 3) == 1Comparison
Section titled “Comparison”print(1 < 2) # trueprint(2 <= 2) # trueprint(3 > 4) # falseprint("a" == "a") # trueprint("a" != "b") # true<, <=, >, and >= work on numbers only. Comparing strings with them raises TypeError. Use == and != for strings.
Equality follows the value model:
- Ints and floats compare numerically across types.
true == 1is false.- Strings compare by content.
- Reference types compare by identity.
See Types & Values for the full rules.
Logical operators
Section titled “Logical operators”and and or short-circuit and return one of their operands, not necessarily a bool. not always returns a bool.
print(0 or 5) # 5print(0 and 5) # 0print(not 0) # trueThis makes and and or useful for defaults and guards:
let name = maybe_name or "anonymous"Assignment
Section titled “Assignment”Assignment is an expression that returns the assigned value.
let x = 1let y = (x = 5)print(x) # 5print(y) # 5Compound assignment operators exist for arithmetic and bitwise operations:
x += 1x -= 2x *= 3x /= 4x &= 0x0Fx |= 0x10x ^= 0x03x <<= 1x >>= 1There is no ++ or --. Use x += 1.
Ranges
Section titled “Ranges”let r = 1..10for i in r { print(i) # 1 through 9}The end is exclusive. start > end produces an empty range. Boundaries must be integers.
Access and calls
Section titled “Access and calls”let values = [10, 20, 30]print(values[1]) # 20print(values.slice(0, 2))
let person = {name: "Ada"}print(person.name)print(person["name"])
fn add(a, b) => a + bprint(add(2, 3))Method calls bind self automatically for struct instances. Keyword arguments are supported at call sites:
struct Point(x, y) {}let p = Point(x: 1, y: 2)