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)

LevelOperatorsAssociativityProtocol
1. (member access)left
2() [] (call, subscript)left
3- + ! not (unary prefix)rightNegatable, —, BitwiseNot, Not
4<< >>leftLeftShift, RightShift
5* / % &leftMultipliable, Divisible, Modulo, BitwiseAnd
6+ - | ^leftAddable, Subtractable, BitwiseOr, BitwiseXor
7..< ..= (range)leftRangeConstructible, ClosedRangeConstructible
8== !=leftEquatable (raw: Equal, NotEqual)
9< <= > >=leftComparable (raw: Less, LessOrEqual, Greater, GreaterOrEqual)
10andleftAnd (short-circuit)
11??rightCoalesce
12orleftOr (short-circuit)
13= += -= *= /= %= &= |= ^= <<= >>=right

Notes

  • Kestrel uses keyword-style logical operators: and, or, not — not &&, ||, !.
  • ! is the bitwise NOT operator (flips all bits), backed by BitwiseNot. For logical negation, use not.
  • and and or are short-circuit: the right operand is passed as a thunk and only evaluated when needed. They are backed by the And and Or protocols, so any conforming type can use them — they are not restricted to Bool.
  • Comparison operators share a precedence level, so chaining like a < b < c parses as (a < b) < c, which is a type error. Write a < b and b < c instead.
  • Compound-assignment operators (+=, etc.) require the corresponding arithmetic/bitwise protocol and var on the left-hand side.
  • Subscript (obj(key)) is special: defined per-type via the subscript declaration. 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.