Skip to main content

Expressions

This document describes expression evaluation rules in Mux.

Expression Classification

Expressions are categorized by their evaluation behavior:

CategoryDescriptionExamples
Value expressionsProduce a valueLiterals, variables, operators
Place expressionsDenote storage locationsVariables, array elements, fields
Void expressionsProduce no valuesome function calls

Primary Expressions

Literals

snippet.mux
Loading...

Identifiers

snippet.mux
Loading...

Parenthesized Expressions

snippet.mux
Loading...

Tuple Literals

Tuples are fixed size pairs with exactly two elements:

snippet.mux
Loading...

Access tuple fields with .left and .right:

snippet.mux
Loading...

List Literals

snippet.mux
Loading...

Type Inference for Lists

The element type is inferred from the contents:

snippet.mux
Loading...

Empty lists require explicit type annotation:

snippet.mux
Loading...

Map Literals

snippet.mux
Loading...

Type Inference

snippet.mux
Loading...

Set Literals

snippet.mux
Loading...

Type Inference

snippet.mux
Loading...

Lambda Expressions

snippet.mux
Loading...

Lambda Capture

Lambdas capture variables from enclosing scope:

snippet.mux
Loading...

Enum Instantiation

snippet.mux
Loading...

Class Instantiation

snippet.mux
Loading...

Generic Classes

snippet.mux
Loading...

Member Access

snippet.mux
Loading...

Field Access

snippet.mux
Loading...

Method Call

snippet.mux
Loading...

Array/List Access

snippet.mux
Loading...

Safe Access with .get()

snippet.mux
Loading...

Range Expressions

snippet.mux
Loading...

Conditional Expressions

Mux does not have a ternary ? : operator. Use if/else as a statement:

snippet.mux
Loading...

Expression Statements

Expressions can be used as statements when side effects are desired:

snippet.mux
Loading...

Type of Expressions

The compiler determines the type of each expression:

ExpressionType
Integer literalint
Float literalfloat
String literalstring
Boolean literalbool
Tuple literaltuple<T, U>
List literallist<T> (inferred)
IdentifierDeclared or inferred type
a + bType of a and b (must match)
a == bbool
a && bbool
!abool
range(a, b)list<int>
Lambdafunc(...) -> ...

See Also