Open Manual

<!-- nytrix-doc: {"audience":"user","featured":false,"group":"spec","order":160,"summary":"Operator precedence, comparisons, arithmetic, assignment, and overload rules."} -->

Operators

Operators cover arithmetic, comparison, logic, bitwise work, ternary selection,

coalescing, indexing, calls, and member access.

Arithmetic

a + b
a - b
a * b
a / b
a % b
a ^ b

Arithmetic operators work on numeric values that the active type and runtime

path accept. The value type and compiler mode choose overflow, widening, and

native-code behavior.

^ is exponentiation. It is right-associative, so 2^3^2 parses as

2^(3^2).

Nytrix has no +% or *% wrapping operators. Unsigned arithmetic uses

the ordinary operators and wraps on overflow.

Precedence

Higher rows bind more tightly.

LevelOperatorsAssociativity
8^right
7&, , bitwise xor, <<, >>left
6*, /, %left
5+, -left
4<, <=, >, >=, lo..hi rangeleft
3==, !=left
2&&, ??left
1, a> fleft
-cond ? a : b, assignment = and compound formsright

Consequences worth noting:

a & b + c is a & (b + c).

1..n == 1..n compares two range values.

x == (y ?? z).

a ? b : (c = d).

Parentheses override any of these rules:

(a + b) * c
(flags & mask) != 0
cond ? a : (b ?? fallback)

Custom operators

Declare custom operators on a type with an impl block:

impl Meter {
   operator + self: self = add
   operator ^ int: self = pow
   operator ^^ self: self = xor
   operator == self: bool = same
}

The operator body is a named function. The colon separates the right operand

type from the return type; parameters elsewhere use the normal Type name

spelling. Operator overloading never changes the fixed precedence of the

operator spelling; it only supplies a definition for a type.

Related