Skip to main content

Modules and Imports

Mux uses Python-style imports for code organization and reuse.

Basic Import Syntax

basic_import.mux
Loading...
  • 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:

stdlib_imports.mux
Loading...
  • import std exposes module namespaces like std.assert, std.math, std.io, std.random, std.datetime, std.sync, and std.net
  • import std.<module> imports a single stdlib module namespace
  • import std.* performs a flat import of stdlib items into the current scope

Module Resolution

Module paths map to file paths:

module_resolution.mux
Loading...

File Structure:

project/
├── main.mux
├── math.mux
├── shapes/
│   ├── circle.mux
│   └── rectangle.mux
└── lib/
    └── core/
        └── util.mux

Using Imports

Basic Usage

math_module.mux
Loading...
main_import.mux
Loading...

Aliased Imports

Use as to rename imported modules:

aliased_imports.mux
Loading...

Imports for Side Effects

Use _ when importing only for side effects:

side_effect_import.mux
Loading...

Name Mangling

Functions from imported modules use mangled names to prevent conflicts:

fibonacci_module.mux
Loading...
using_imported_func.mux
Loading...

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:

module_init.mux
Loading...

The compiler:

  1. Generates a module init function
  2. Calls it before main() executes
  3. Ensures each module initializes only once

Dependency Resolution

The compiler processes modules in dependency order:

module_a.mux
Loading...
module_b.mux
Loading...
module_c.mux
Loading...

Initialization order: c -> b -> a -> main

Module Scope

Public by Default

All functions and types are public by default:

public_module.mux
Loading...

Module-Level Variables

module_variables.mux
Loading...
using_module_vars.mux
Loading...

Circular Dependencies

Warning: Avoid circular imports:

circular_a.mux
Loading...
circular_b.mux
Loading...

Solution: Restructure to remove circular dependencies or extract shared code to a third module.

Common Module Patterns

Utility Module

utility_module.mux
Loading...

Type Module

type_module.mux
Loading...

Constants Module

constants_module.mux
Loading...

Module Organization

Flat Structure

For small projects:

project/
├── main.mux
├── math.mux
├── utils.mux
└── constants.mux

Hierarchical 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.mux

Import Best Practices

  1. Import at the top - All imports at file beginning
  2. Avoid circular dependencies - Restructure if needed
  3. Use descriptive module names - math not m
  4. Alias long names - import very.long.module.path as path
  5. Group related functions - Put related utilities in same module
  6. One module per file - Keep file structure simple
  7. Document public APIs - Comment exported functions
  8. Avoid deep nesting - Keep module hierarchy shallow

Example Project

calculator/
├── main.mux
├── operations/
│   ├── basic.mux
│   └── advanced.mux
└── utils/
    └── format.mux
basic_operations.mux
Loading...
advanced_operations.mux
Loading...
format_module.mux
Loading...
calculator_main.mux
Loading...

Technical Implementation

Module Compilation

  1. Parse imports - Extract all import statements
  2. Build dependency graph - Determine module order
  3. Process modules - Parse and analyze in topological order
  4. Generate init functions - Create module initialization code
  5. Link modules - Combine into final executable

Name Mangling Details

Module functions are prefixed with their module path:

shapes_circle.mux
Loading...

Generates: shapes_circle_area (module path becomes prefix)

See Also