Operators
This document describes all operators in Mux, including their precedence, associativity, and behavior.
Operator Precedence
Higher precedence operators are evaluated first.
| Precedence | Operators | Associativity |
|---|---|---|
| 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 |
| 9 | in | Left-to-right |
Arithmetic Operators
Binary Arithmetic
| Operator | Description | Types | Example |
|---|---|---|---|
+ | Addition | int, float, string | 5 + 3, "a" + "b" |
- | Subtraction | int, float | 10 - 4 |
* | Multiplication | int, float | 6 * 7 |
/ | Division | int, float | 15 / 3 |
% | Modulo | int, float | 10 % 3 (result: 1) |
** | Exponentiation | int, float | 2 ** 3 (result: 8) |
Arithmetic Rules
- All arithmetic operators require both operands to have the same type
- No implicit type conversion between
intandfloat - Division by zero is a runtime error for
int, returnsinfforfloat - Modulo with negative numbers follows C semantics
- Arithmetic operators are builtin-only for primitive numeric types (not interface-dispatched)
Exponentiation Details
Properties:
- Right-associative:
a ** b ** c=a ** (b ** c) - Higher precedence than
*,/,%
Increment and Decrement
| Operator | Description | Restrictions |
|---|---|---|
++ | Postfix increment | int only, standalone statement |
-- | Postfix decrement | int only, standalone statement |
Usage Rules
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
| Operator | Description | Types |
|---|---|---|
== | Equality | int, float, bool, char, string, list, map, set, tuple, optional, result, user enums, and classes declaring Equatable, Comparable or Hashable |
!= | Inequality | same as == |
< | Less than | int, float, string, and classes declaring Comparable |
<= | Less than or equal | int, float, string, and classes declaring Comparable |
> | Greater than | int, float, string, and classes declaring Comparable |
>= | Greater than or equal | int, 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
mapand asetignore insertion order.
What does not compare
A class does not compare with == unless it says so. Declaring
is Equatable and writing eq is what opts in:
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
| Operator | Description | Behavior |
|---|---|---|
&& | Logical AND | Short-circuit evaluation |
|| | Logical OR | Short-circuit evaluation |
! | Logical NOT | Unary negation |
Short-Circuit Evaluation
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 resultPhi nodes merge results from different branches.
Membership Operator
| Operator | Description | Types |
|---|---|---|
in | Membership test | T in list<T>, T in set<T>, string in string, char in string |
Collection Operators
Concatenation with +
| Types | Operation | result |
|---|---|---|
list<T> + list<T> | Concatenation | Combined list |
map<K,V> + map<K,V> | Merge | Combined map |
set<T> + set<T> | Union | Set with all elements |
string + string | Concatenation | Combined string |
Compound Assignment Operators
| Operator | Expansion | Types |
|---|---|---|
+= | a = a + b | int, float, string |
-= | a = a - b | int, float |
*= | a = a * b | int, float |
/= | a = a / b | int, float |
%= | a = a % b | int, float |
Reference Operators
| Operator | Description | Example |
|---|---|---|
& | Create reference | auto r = &x |
* | Dereference | *r = 42 |
See Also
- Expressions - Expression evaluation rules