Skip to main content

Operators

This document describes all operators in Mux, including their precedence, associativity, and behavior.

Operator Precedence

Higher precedence operators are evaluated first.

PrecedenceOperatorsAssociativity
1 (highest). (member access), (), []Left-to-right
2**Right-to-left
3!Right-to-left
4*, /, %Left-to-right
5+, - (binary)Left-to-right
6<, <=, >, >=Left-to-right
7==, !=Left-to-right
8&&, ||Left-to-right
9inLeft-to-right

Arithmetic Operators

Binary Arithmetic

OperatorDescriptionTypesExample
+Additionint, float, string5 + 3, "a" + "b"
-Subtractionint, float10 - 4
*Multiplicationint, float6 * 7
/Divisionint, float15 / 3
%Moduloint, float10 % 3 (result: 1)
**Exponentiationint, float2 ** 3 (result: 8)

Arithmetic Rules

  • All arithmetic operators require both operands to have the same type
  • No implicit type conversion between int and float
  • Division by zero is a runtime error for int, returns inf for float
  • Modulo with negative numbers follows C semantics
  • Arithmetic operators are builtin-only for primitive numeric types (not interface-dispatched)

Exponentiation Details

Mux
Loading...

Properties:

  • Right-associative: a ** b ** c = a ** (b ** c)
  • Higher precedence than *, /, %

Increment and Decrement

OperatorDescriptionRestrictions
++Postfix incrementint only, standalone statement
--Postfix decrementint only, standalone statement

Usage Rules

Mux
Loading...

Rationale

The postfix-only, standalone-only design prevents ambiguity and side-effect confusion that can occur with prefix operators or expression-embedded increments.

Comparison Operators

OperatorDescriptionTypes
==Equalityint, float, bool, char, string, list, map, set, tuple, optional, result, user enums, and classes declaring Equatable, Comparable or Hashable
!=Inequalitysame as ==
<Less thanint, float, string, and classes declaring Comparable
<=Less than or equalint, float, string, and classes declaring Comparable
>Greater thanint, float, string, and classes declaring Comparable
>=Greater than or equalint, float, string, and classes declaring Comparable

Comparison Rules

  • Both operands must have the same type
  • No implicit conversion between numeric types
  • String comparison is lexicographic (Unicode codepoint order)
  • Collections and enums compare structurally - by their contents, not by identity. Two lists with the same elements are equal; two enum values are equal when they are the same variant carrying equal payloads. A map and a set ignore insertion order.
Mux
Loading...

What does not compare

A class does not compare with == unless it says so. Declaring is Equatable and writing eq is what opts in:

Mux
Loading...

is Comparable (which supplies cmp) and is Hashable both grant equality too, so a class needs only one of the three. See Classes for the full set.

References, function values, and the result of a void call are never comparable. Each is reported with a suggestion at the point of use.

Ordering

<, <=, > and >= accept int, float and string, plus any class declaring is Comparable. There is no ordering for collections, enums, optional or result - compare them with ==, or match on them.

Logical Operators

OperatorDescriptionBehavior
&&Logical ANDShort-circuit evaluation
||Logical ORShort-circuit evaluation
!Logical NOTUnary negation

Short-Circuit Evaluation

Mux
Loading...

Implementation

The && and || operators use LLVM control flow for short-circuit evaluation:

For `a && b`:
1. Evaluate a
2. If a is false, return false (b not evaluated)
3. If a is true, evaluate b and return result

Phi nodes merge results from different branches.

Membership Operator

OperatorDescriptionTypes
inMembership testT in list<T>, T in set<T>, string in string, char in string
Mux
Loading...

Collection Operators

Concatenation with +

TypesOperationresult
list<T> + list<T>ConcatenationCombined list
map<K,V> + map<K,V>MergeCombined map
set<T> + set<T>UnionSet with all elements
string + stringConcatenationCombined string
Mux
Loading...

Compound Assignment Operators

OperatorExpansionTypes
+=a = a + bint, float, string
-=a = a - bint, float
*=a = a * bint, float
/=a = a / bint, float
%=a = a % bint, float
Mux
Loading...

Reference Operators

OperatorDescriptionExample
&Create referenceauto r = &x
*Dereference*r = 42
Mux
Loading...

See Also