Operators
Mux provides a comprehensive set of operators for arithmetic, comparison, logical operations, and more.
Arithmetic Operators
Standard arithmetic operations with strict type requirements:
| Operator | Description | Types | Example |
|---|---|---|---|
+ | Addition | int, float, string | 5 + 3 -> 8 |
- | Subtraction | int, float | 10 - 4 -> 6 |
* | Multiplication | int, float | 6 * 7 -> 42 |
/ | Division | int, float | 15 / 3 -> 5 |
% | Modulo | int, float | 10 % 3 -> 1 |
** | Exponentiation | int, float | 2 ** 3 -> 8 |
Examples
Exponentiation (**)
- Right-associative:
2 ** 3 ** 2equals2 ** (3 ** 2)= 512 - Higher precedence than
*and/:2 * 3 ** 2equals2 * 9= 18 - Works on both
intandfloattypes
Type Constraints
As previously stated, No implicit conversions, operands must be the same type:
Increment and Decrement
Postfix-only operators for incrementing/decrementing:
| Operator | Description | Example |
|---|---|---|
++ | Increment (postfix only) | counter++ |
-- | Decrement (postfix only) | counter-- |
Design Constraints
- Postfix only:
counter++is valid,++counteris NOT supported - Standalone only: Must appear on their own line, not within expressions
- Only on mutable variables: Cannot be used on
constor literals - Type preservation: Operates on
inttypes only
Rationale: The postfix-only, standalone-only design prevents ambiguity and side-effect confusion.
Comparison Operators
Compare values of compatible types:
| Operator | Description | Types | Example |
|---|---|---|---|
== | Equality | All comparable types | a == b |
!= | Inequality | All comparable types | a != b |
< | Less than | int, float, string | 5 < 10 |
<= | Less than or equal | int, float, string | x <= 100 |
> | Greater than | int, float, string | y > 0 |
>= | Greater than or equal | int, float, string | age >= 18 |
Examples
Type Constraints
Both operands must have the same type:
Logical Operators
Boolean operations with short-circuit evaluation:
| Operator | Description | Example |
|---|---|---|
&& | Logical AND (short-circuit) | a && b |
|| | Logical OR (short-circuit) | a || b |
! | Logical NOT | !flag |
Short-Circuit Evaluation
&&only evaluates right side if left istrue||only evaluates right side if left isfalse
How Short-Circuiting Works
Mux uses LLVM control flow for short-circuit evaluation:
Generates:
- Evaluate
a - If
ais false, result is false (skipb) - If
ais true, evaluateband use its value - Phi node merges results from different paths
This enables:
- Performance optimization (skip unnecessary checks)
- Safe null/bounds checking patterns
- Branch prediction opportunities
Membership Operator (in)
Test for membership/containment:
| Left Operand | Right Operand | Description |
|---|---|---|
T | list<T> | Check if value exists in list |
T | set<T> | Check if value exists in set |
string | string | Check if substring exists |
char | string | Check if character exists in string |
Type Constraints
Both operands must have compatible element types:
Collection Operators
Concatenation with +
The + operator is overloaded for collection types:
| Types | Operation | result |
|---|---|---|
list<T> + list<T> | Concatenation | Combined list |
map<K,V> + map<K,V> | Merge | Combined map (latter overwrites on collision) |
set<T> + set<T> | Union | Set with all unique elements |
string + string | Concatenation | Combined string |
Examples
Type Constraints
Collections must be the exact same type:
Reference Operators
Create and dereference references:
| Operator | Description | Example |
|---|---|---|
& | Create reference | &variable |
* | Dereference | *reference |
Examples
Operator Precedence
From highest to lowest precedence:
- Unary:
!,*(dereference),&(reference) - Exponentiation:
**(right-associative) - Multiplicative:
*,/,% - Additive:
+,- - Comparison:
<,<=,>,>= - Equality:
==,!= - Logical AND:
&& - Logical OR:
||
Examples
Operator Overloading
Arithmetic operators (+, -, *, /, %, **) are builtin-only for primitive numeric types.
They are not dispatched through Add/Sub/Mul/Div interfaces.
Custom types can implement interfaces for equality and comparison:
| Operator | Interface |
|---|---|
==, != | Equatable |
<, >, <=, >= | Comparable |
Custom Type Example
Assignment Operators
Simple assignment (no compound assignment):
| Operator | Description | Example |
|---|---|---|
= | Assignment | x = 10 |
Note: Mux does NOT support compound assignment operators like +=, -=, etc.
Best Practices
- Use parentheses for clarity - Don't rely on precedence rules
- Explicit type conversions - No implicit conversions allowed
- Short-circuit for efficiency - Use
&&and||wisely - Use
infor membership - Cleaner than calling methods - Prefer
++/--on separate lines - Clearer than expression-embedded - Use interface-based comparison thoughtfully - Implement
Equatable/Comparableonly when needed - Understand right-associativity of
**- Use parentheses if unsure
See Also
- Types - Type conversions and constraints
- Generics - Built-in interfaces for operators
- Collections - Collection operators
- Functions - Operator overloading with interfaces