Open Manual

Operators

Operators cover arithmetic, comparison, logic, bitwise work, ternary selection, coalescing, indexing, calls, and member access.

Contents

Arithmetic

Operator spellings:

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

Rules

  1. Parentheses override the precedence table; custom declarations never change precedence.
  2. ** and ^^ are right-associative exponentiation spellings; ^ is bitwise XOR.
  3. >> is arithmetic for signed integers. Use bshr when the operation must fill with zero bits.
  4. Nytrix has no wrapping +% or *% operators. Use checked helpers when signed overflow results or saturation are required.

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.

** and ^^ are exponentiation spellings and are right-associative, so 2 ** 3 ** 2 parses as 2 ** (3 ** 2). ^^ is an alias for **; ^ is bitwise XOR. Since legacy ^ power and modern ^ XOR are lexically indistinguishable, migration requires an explicit dialect: ny fmt --migrate-operators --assume-legacy-operators path converts legacy ^ power and ^^ XOR to ** and ^. Without --assume-legacy-operators, the migration command leaves ambiguous expressions unchanged. It preserves strings and comments; for .nshape fixtures it rewrites only embedded source ny blocks.

Nytrix has no +% or *% wrapping operators. Unsigned arithmetic uses the ordinary operators and wraps on overflow. For signed 64-bit operations that need explicit overflow results or saturation, use the checked helpers in core.math.checked.

Bitwise and shift operations

^ is bitwise XOR.

>> is an arithmetic right shift: it preserves the sign of negative integers. A negative shift count leaves the value unchanged. For native signed 64-bit values, a count of 64 or more yields the sign-fill value (0 or -1); BigInt values continue to shift at arbitrary precision. The bshr helper is a logical right shift and fills with zero bits.

Precedence

Higher rows bind more tightly.

LevelOperatorsAssociativity
8**, ^^right
7&, |, ^, <<, >>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:

Parentheses override any of these rules:

use core

def grouped = (1 + 2) * 3
def masked = (5 & 1) != 0
def selected = false ? 1 : (nil ?? 4)

assert(grouped == 9 && masked && selected == 4,
       "parentheses make expression grouping explicit")

Custom operators

Operator overloading binds a fixed spelling (such as +) to a target function for a (left type, right type) pair. Precedence never changes.

Declare custom operators on a type with an impl block (left operand type is the impl owner). Full grammar, top-level declarations, and trait-adjacent rules are in Traits and impl.

use core

impl Meter {
   fn add(self left, self right) self {
      Meter({"value": int(left.get("value", 0)) +
                    int(right.get("value", 0))})
   }
   operator + self: self = add
}

def Meter left = Meter({"value": 19})
def Meter right = Meter({"value": 23})
def Meter total = left + right
assert(int(total.get("value", 0)) == 42,
       "the operator uses its declared target")

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.