Operators
Full operator table with precedence, associativity, and the protocol that backs each one. To define operators on your own types, see Functions → Operator Overloading.
Precedence (high to low)
| Level | Operators | Associativity | Protocol |
|---|---|---|---|
| 1 | . (member access) | left | — |
| 2 | () [] (call, subscript) | left | — |
| 3 | - + ! not (unary prefix) | right | Negatable, —, BitwiseNot, Not |
| 4 | << >> | left | LeftShift, RightShift |
| 5 | * / % & | left | Multipliable, Divisible, Modulo, BitwiseAnd |
| 6 | + - | ^ | left | Addable, Subtractable, BitwiseOr, BitwiseXor |
| 7 | ..< ..= (range) | left | RangeConstructible, ClosedRangeConstructible |
| 8 | == != | left | Equatable (raw: Equal, NotEqual) |
| 9 | < <= > >= | left | Comparable (raw: Less, LessOrEqual, Greater, GreaterOrEqual) |
| 10 | and | left | And (short-circuit) |
| 11 | ?? | right | Coalesce |
| 12 | or | left | Or (short-circuit) |
| 13 | = += -= *= /= %= &= |= ^= <<= >>= | right | — |
Notes
- Kestrel uses keyword-style logical operators:
and,or,not— not&&,||,!. !is the bitwise NOT operator (flips all bits), backed byBitwiseNot. For logical negation, usenot.andandorare short-circuit: the right operand is passed as a thunk and only evaluated when needed. They are backed by theAndandOrprotocols, so any conforming type can use them — they are not restricted toBool.- Comparison operators share a precedence level, so chaining like
a < b < cparses as(a < b) < c, which is a type error. Writea < b and b < cinstead. - Compound-assignment operators (
+=, etc.) require the corresponding arithmetic/bitwise protocol andvaron the left-hand side. - Subscript (
obj(key)) is special: defined per-type via thesubscriptdeclaration. Square brackets are reserved for type parameters. See Structs → Subscripts. ..<and..=also have unary prefix forms for open-ended ranges (e.g.,..<n,..=n).
Custom operators
Kestrel does not currently support user-defined operator symbols. Operator support is fixed to the table above; conform your type to the relevant protocol to give those symbols meaning.