Modules and Imports
Mux uses Python-style imports for code organization and reuse.
Basic Import Syntax
- Python-style imports only
- Module paths map directly to file paths
- Imported symbols can be used immediately
Standard Library Imports
The stdlib uses the std namespace:
import stdexposes module namespaces likestd.assert,std.math,std.io,std.random,std.datetime,std.sync, andstd.netimport std.<module>imports a single stdlib module namespaceimport std.*performs a flat import of stdlib items into the current scope
Module Resolution
Module paths map to file paths:
File Structure:
project/
├── main.mux
├── math.mux
├── shapes/
│ ├── circle.mux
│ └── rectangle.mux
└── lib/
└── core/
└── util.muxUsing Imports
Basic Usage
Aliased Imports
Use as to rename imported modules:
Imports for Side Effects
Use _ when importing only for side effects:
Name Mangling
Functions from imported modules use mangled names to prevent conflicts:
Generates LLVM function: math_fibonacci (not just fibonacci)
This prevents conflicts when multiple modules define functions with the same name.
Module Initialization
Top-level statements in modules become initialization functions:
The compiler:
- Generates a module init function
- Calls it before
main()executes - Ensures each module initializes only once
Dependency Resolution
The compiler processes modules in dependency order:
Initialization order: c -> b -> a -> main
Module Scope
Public by Default
All functions and types are public by default:
Module-Level Variables
Circular Dependencies
Warning: Avoid circular imports:
Solution: Restructure to remove circular dependencies or extract shared code to a third module.
Common Module Patterns
Utility Module
Type Module
Constants Module
Module Organization
Flat Structure
For small projects:
project/
├── main.mux
├── math.mux
├── utils.mux
└── constants.muxHierarchical Structure
For larger projects:
project/
├── main.mux
├── lib/
│ ├── core/
│ │ ├── math.mux
│ │ └── utils.mux
│ ├── data/
│ │ ├── parser.mux
│ │ └── validator.mux
│ └── net/
│ ├── http.mux
│ └── tcp.mux
└── config/
└── settings.muxImport Best Practices
- Import at the top - All imports at file beginning
- Avoid circular dependencies - Restructure if needed
- Use descriptive module names -
mathnotm - Alias long names -
import very.long.module.path as path - Group related functions - Put related utilities in same module
- One module per file - Keep file structure simple
- Document public APIs - Comment exported functions
- Avoid deep nesting - Keep module hierarchy shallow
Example Project
calculator/
├── main.mux
├── operations/
│ ├── basic.mux
│ └── advanced.mux
└── utils/
└── format.muxTechnical Implementation
Module Compilation
- Parse imports - Extract all import statements
- Build dependency graph - Determine module order
- Process modules - Parse and analyze in topological order
- Generate init functions - Create module initialization code
- Link modules - Combine into final executable
Name Mangling Details
Module functions are prefixed with their module path:
Generates: shapes_circle_area (module path becomes prefix)