Expressions and Operators
Precedence, arithmetic, comparison, conversion, calls, and indexing.
Contents
Assignment is a statement, not an expression.
Precedence#
Operators are listed from lowest to highest precedence. Binary operators are left-associative. Parentheses may be used to make grouping explicit.
| Level | Operators | Notes |
|---|---|---|
| 1 | \|\| |
Short-circuit logical or |
| 2 | && |
Short-circuit logical and |
| 3 | \| |
Bitwise or |
| 4 | ^ |
Bitwise exclusive or |
| 5 | & |
Bitwise and |
| 6 | ==, != |
Equality |
| 7 | is |
Pattern test |
| 8 | <, <=, >, >= |
Ordering |
| 9 | <<, >> |
Shifts |
| 10 | +, - |
Addition, subtraction, string and list concatenation |
| 11 | *, /, % |
Multiplication, division, remainder |
| 12 | -, !, ~ |
Unary minus, logical not, bitwise complement |
| 13 | call, field, index, ? |
Postfix forms |
The right-hand side of is is a pattern. ? applies to the expression immediately to its left.
Arithmetic#
Integer arithmetic unifies operand types. If both operands have the same signedness, the result uses the wider rank, with a minimum of i32 or u32.
| Expression | Result type | Note |
|---|---|---|
i32 + i32 |
i32 |
No automatic promotion to i64 |
i8 + i8 |
i32 |
Narrow integers are promoted for arithmetic |
i32 + i64 |
i64 |
Wider signed operand wins |
u32 + u32 |
u32 |
Unsigned arithmetic |
i32 + u32 |
u32 or i64 depending on rank |
Prefer explicit conversion |
f64 + f64 |
f64 |
The only floating-point type |
i32 + f64 |
f64 |
Numeric coercion applies |
Narrowing is never implicit. Assigning an i64 to an i32 requires an explicit conversion. A constant is checked for range at compile time.
var wide i64 = 10
var narrow i32 = trunc[i32](wide)
var widened i64 = cast[i64](narrow)
% is defined for integers only. Unary minus requires a signed integer or f64; unary complement requires an integer; ! requires bool.
Compile-time folding rejects a constant result that does not fit the target type. At runtime, use a wider type or an explicit check when overflow is possible.
Comparison#
Ordering comparisons require two numeric operands of compatible type, or two runes. They produce bool. Equality compares integers, floats, booleans, strings, runes, pointers, C strings, and enum values of the same type. Records, lists, closures, and sum values are not comparable with ==.
const same = "haze" == "haze"
const ordered = 3 < 8
const colour = Color.Red == Color.Red
A type that needs structural equality should expose a function or implement a trait such as Eq.
Logical Operators#
&& and || require bool operands and short-circuit.
fn safe_ratio(n i32, d i32) i32
: pre d != 0
=> if d == 0 { 0 } else { n / d }
Bitwise and Shift#
&, |, ^, and ~ require integer operands. << and >> require an integer value and an integer shift count. Narrow operands are promoted to i32 or u32. A right shift of an unsigned value is logical.
const mask = 0xFF
const low = (value & mask)
const doubled = value << 1
const high = cast[u32](value) >> 24
Bitwise operators are not defined for f64. Shift counts are not checked statically; keep them in range as a precondition when the input is untrusted.
Conversion#
cast[T](value) converts between numeric types and widens integers. trunc[T](value) narrows an integer to a strictly narrower type. truncate is a synonym. Both require the target type in square brackets.
const as_float = cast[f64](42)
const as_u64 = cast[u64](42)
const small = trunc[u8](300)
const back = cast[i32](small)
Casting between str and a numeric type is rejected. A narrowing cast without trunc is rejected even when the value would fit.
Calls#
fn scale(value i32, factor i32) i32 => value * factor
fn main() {
print(scale(3, 4))
print((3).scale(4))
}
receiver.name(arguments) is rewritten to name(receiver, arguments). This works for free functions and trait methods. The receiver is an ordinary first argument.
Field Access and Indexing#
Records expose fields with .. Lists, strings, and byte buffers support indexing with [...]. len is available on lists as i32, and on strings and bytes as u64.
type Point {
x i32
y i32
}
fn main() {
const p = Point { x = 1, y = 2 }
const xs = [10, 20, 30]
print(p.x)
print(xs.len)
print(xs[1])
}
Indexing performs a runtime bounds check. Index assignment is permitted for lists and byte buffers; strings are immutable. get(list, index) returns Option[T] instead of failing. It may be called as list.get(index) when no user-defined get shadows it.
const xs = [10, 20, 30]
if xs.get(5) is Some(value) {
print(value)
} else {
print("out of range")
}
Conditional and Match Expressions#
An if with both branches is an expression whose type is the common type of the branches. A missing else makes it a statement. A match is an expression when every arm produces a value.
const label = if n > 0 { "positive" } else { "non-positive" }
const text = match value {
Some(v) => $"found {v}"
None => "missing"
}
All value-producing arms of a match must have compatible types. See Pattern Matching.
Block Expressions#
A block in expression position produces the value of its final expression, or void if the final statement produces no value.
const result = {
const base = 10
const extra = 5
base + extra
}
The ? Postfix#
? unwraps a Result when it is Success and returns the contained Error from the enclosing function when it is Error. After a record literal, it requests checked construction, as in Positive { n }?. See Option and Result and Checked Construction.
fn twice(value Result[i32, str]) Result[i32, str] {
const n = value?
return Success(n * 2)
}
The enclosing function must return a Result with a compatible error type. There is no unwrap.
Printing#
print accepts integers, floats, booleans, runes, and strings. Records, lists, sums, Option, and Result are not directly printable; interpolate their components.
print(42)
print(3.5)
print("text")
print($"point = {p.x}, {p.y}")