Skip to main content

Operators

Mux provides a comprehensive set of operators for arithmetic, comparison, logical operations, and more.

Arithmetic Operators

Standard arithmetic operations with strict type requirements:

OperatorDescriptionTypesExample
+Additionint, float, string5 + 3 -> 8
-Subtractionint, float10 - 4 -> 6
*Multiplicationint, float6 * 7 -> 42
/Divisionint, float15 / 3 -> 5
%Moduloint, float10 % 3 -> 1
**Exponentiationint, float2 ** 3 -> 8

Examples

arithmetic_ops.mux
Loading...

Exponentiation (**)

  • Right-associative: 2 ** 3 ** 2 equals 2 ** (3 ** 2) = 512
  • Higher precedence than * and /: 2 * 3 ** 2 equals 2 * 9 = 18
  • Works on both int and float types
exponentiation.mux
Loading...

Type Constraints

As previously stated, No implicit conversions, operands must be the same type:

type_constraints.mux
Loading...

Increment and Decrement

Postfix-only operators for incrementing/decrementing:

OperatorDescriptionExample
++Increment (postfix only)counter++
--Decrement (postfix only)counter--

Design Constraints

  • Postfix only: counter++ is valid, ++counter is NOT supported
  • Standalone only: Must appear on their own line, not within expressions
  • Only on mutable variables: Cannot be used on const or literals
  • Type preservation: Operates on int types only
increment_decrement.mux
Loading...

Rationale: The postfix-only, standalone-only design prevents ambiguity and side-effect confusion.

Comparison Operators

Compare values of compatible types:

OperatorDescriptionTypesExample
==EqualityAll comparable typesa == b
!=InequalityAll comparable typesa != b
<Less thanint, float, string5 < 10
<=Less than or equalint, float, stringx <= 100
>Greater thanint, float, stringy > 0
>=Greater than or equalint, float, stringage >= 18

Examples

comparison_ops.mux
Loading...

Type Constraints

Both operands must have the same type:

comparison_types.mux
Loading...

Logical Operators

Boolean operations with short-circuit evaluation:

OperatorDescriptionExample
&&Logical AND (short-circuit)a && b
||Logical OR (short-circuit)a || b
!Logical NOT!flag

Short-Circuit Evaluation

  • && only evaluates right side if left is true
  • || only evaluates right side if left is false
short_circuit.mux
Loading...

How Short-Circuiting Works

Mux uses LLVM control flow for short-circuit evaluation:

short_circuit_codegen.mux
Loading...

Generates:

  1. Evaluate a
  2. If a is false, result is false (skip b)
  3. If a is true, evaluate b and use its value
  4. 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 OperandRight OperandDescription
Tlist<T>Check if value exists in list
Tset<T>Check if value exists in set
stringstringCheck if substring exists
charstringCheck if character exists in string

Type Constraints

Both operands must have compatible element types:

membership_types.mux
Loading...

Collection Operators

Concatenation with +

The + operator is overloaded for collection types:

TypesOperationresult
list<T> + list<T>ConcatenationCombined list
map<K,V> + map<K,V>MergeCombined map (latter overwrites on collision)
set<T> + set<T>UnionSet with all unique elements
string + stringConcatenationCombined string

Examples

collection_ops.mux
Loading...

Type Constraints

Collections must be the exact same type:

collection_types.mux
Loading...

Reference Operators

Create and dereference references:

OperatorDescriptionExample
&Create reference&variable
*Dereference*reference

Examples

reference_ops.mux
Loading...

Operator Precedence

From highest to lowest precedence:

  1. Unary: !, * (dereference), & (reference)
  2. Exponentiation: ** (right-associative)
  3. Multiplicative: *, /, %
  4. Additive: +, -
  5. Comparison: <, <=, >, >=
  6. Equality: ==, !=
  7. Logical AND: &&
  8. Logical OR: ||

Examples

precedence_example.mux
Loading...

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:

OperatorInterface
==, !=Equatable
<, >, <=, >=Comparable

Custom Type Example

operator_overloading.mux
Loading...

Assignment Operators

Simple assignment (no compound assignment):

OperatorDescriptionExample
=Assignmentx = 10

Note: Mux does NOT support compound assignment operators like +=, -=, etc.

assignment_ops.mux
Loading...

Best Practices

  1. Use parentheses for clarity - Don't rely on precedence rules
  2. Explicit type conversions - No implicit conversions allowed
  3. Short-circuit for efficiency - Use && and || wisely
  4. Use in for membership - Cleaner than calling methods
  5. Prefer ++/-- on separate lines - Clearer than expression-embedded
  6. Use interface-based comparison thoughtfully - Implement Equatable/Comparable only when needed
  7. 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