Reference · 03
Operators & Precedence
Complete operator precedence table, associativity, short-circuit rules, and compound assignment semantics in Ochre.
Operators are listed from highest precedence (tightest binding) to lowest precedence. ochre fmt automatically preserves parenthesization whenever grouping overrides default precedence.
Precedence Table
| Level | Operators | Description | Associativity | ||
|---|---|---|---|---|---|
| 1 | (...), f(...), a.b, a[i], e? | Grouping, call, field access, indexing, try propagation | Left-to-right | ||
| 2 | -x, !x | Unary negation (Int/Float), logical NOT (Bool) | Right-to-left | ||
| 3 | *, /, % | Multiplication, division, remainder | Left-to-right | ||
| 4 | +, - | Addition / Str concatenation, subtraction | Left-to-right | ||
| 5 | .. | Exclusive integer range (start..end) | Non-associative | ||
| 6 | <, <=, >, >= | Ordering comparison (Int, Float, Str) | Left-to-right | ||
| 7 | ==, != | Equality / inequality (matching types) | Left-to-right | ||
| 8 | && | Short-circuit logical AND (Bool) | Left-to-right | ||
| 9 | `\ | \ | ` | Short-circuit logical OR (Bool) | Left-to-right |
| 10 | ?? | Fallback coalescing (Option/Result) | Left-to-right | ||
| 11 | =, +=, -=, *=, /= | Assignment & compound assignment (statements) | Statement |
Operator Rules
- No implicit numeric coercion:
Int + Floatis a compile-time error. Usefloat(a) + bora + int(b). - String concatenation:
+concatenatesStr + Str, returningStr. - Short-circuit guarantees: Both
&&and||skip evaluating the right-hand operand when the left-hand operand determines the outcome — identically in both the bytecode VM and Cranelift AOT. - *Compound assignments (
+=,-=,=,/=): Valid onIntandFloattargets (including nestedvarindices/fields likerows[i][j] += 5). Every object and index expression on the left-hand side is evaluated exact once**.
fn main() {
let a = (1 + 2) * 3
let b = 1 + 2 * 3
let c = 10 - (4 - 1)
println("a={a}, b={b}, c={c}")
}
Expected Output
a=9, b=7, c=7