Skip to main content

Variables and Constants

Mux supports both explicit type declarations and type inference with the auto keyword.

Variable Declarations

Explicit Typing

variables.mux
Loading...

Type Inference with auto

type_inference.mux
Loading...

Important Rules

variable_rules.mux
Loading...

All declarations require either an explicit type or auto with an initializer. Semicolons are not used.

Constants

Constants are immutable values that cannot be reassigned or modified after initialization:

constants.mux
Loading...

Const Enforcement

  • Cannot reassign: const_var = new_value -> ERROR
  • Cannot use compound assignment: const_var += 1 -> ERROR
  • Cannot increment/decrement: const_var++ or const_var-- -> ERROR
  • Applies to both identifiers and class fields
  • Use const when you want a value that won't change after initialization

When to Use auto

  • Local variables with obvious initialization
  • Complex generic types that are clear from context
  • Temporary variables in calculations
  • Iterator variables in loops
explicit_types.mux
Loading...

Using Underscore for Unused Values

The underscore _ is a placeholder for values you don't need:

underscore.mux
Loading...

Best Practice: Use _ when a value is required by syntax but not needed in your code. Don't overuse it when descriptive names would improve readability.

Variable Scope

Variables are scoped to the block in which they are declared:

scope.mux
Loading...

Unless you create a closure, then you can capture variables from the enclosing scope:

closure_scope.mux
Loading...

See Also

  • Types - Type system and conversions
  • Functions - Function declarations
  • Classes - Class fields and constants