Module System
YaoXiang has no import keyword, nor any "declare-then-export" module declarations. A .yx file is a module, and use brings another module's bindings into scope.
This page covers how to use it. For design decisions, see the Module System Specification.
60-Second Quick Start
use std.io
use std.math.{sqrt}
// Whole module import → access via namespace
io.println("hello")
// Specific item import → use directly bare
result = sqrt(16.0)Four Ways to Write Imports
1. Import the Whole Module
use std.io
main = () => {
io.println("Access via the io. prefix")
io.print("Both print and println live in std.io")
}use std.io introduces a namespace, and members are accessed with the io. prefix.
2. Import Only Specific Items
Names listed inside curly braces are bound directly into the current scope, without a prefix:
use std.math.{sqrt, sin, cos}
main = () => {
print(sqrt(16.0))
print(sin(0.0))
}This form is great when you only need a few functions from a module — it saves you from repeatedly typing the prefix while avoiding pulling the entire namespace into scope.
3. Inline Item Aliases
The rename is written inside the curly braces:
use std.io.{print as say}
main = () => {
say("Using say instead of print")
}Aliases Must Go Inside the Curly Braces This form is supported:
use std.io.{print as say} // ✅The one below is rejected — the compiler will suggest the inline form:
use std.io.{print} as say // ❌ Positional alias, not supported4. Module Alias
use helper as h
main = () => {
h.shout("Access helper through h")
}Known Issue: Aliasing Standard Library Modules Crashes
use <project-internal-module> as <alias> works, but use <standard-library-module> as <alias> currently triggers an internal compiler error (E8001 closure function not registered in the function name map):
use std.math as m // ❌ Calling m.sqrt(...) reports E8001Workaround: Use inline item aliases instead — they work fine for standard library modules:
use std.math.{sqrt, sin, cos} // ✅This issue is tracked separately and does not affect the three forms above. :::
Module Directory Organization
mod.yx Is the Directory Entry Point
For a directory to become a module, its entry file must be named mod.yx:
src/
├── main.yx
├── math/
│ ├── mod.yx ← directory entry, module math
│ └── vector.yx ← module math.vector
└── utils/
├── mod.yx ← directory entry, module utils
└── string.yx ← module utils.string// src/helper/mod.yx
use std.io
shout: (msg: string) -> Void = {
io.print(msg)
}
label: () -> string = "src/helper/mod.yx"// src/main.yx
use std.io
use helper
main = () => {
io.print(helper.label())
helper.shout("Cross-module call succeeded")
}index.yx Is Not Recognized
This is the most common pitfall for those coming from Rust. Writing it as index.yx cannot be resolved:
// src/helper/index.yx
label: () => string = "This will fail"use helper // ❌ E5001: module 'helper' not foundReason: Path mapping only recognizes two forms — name.yx and name/mod.yx — and index.yx is not one of them. mod.yx is a convention rather than a hard rule — helper.yx can serve as module helper just as well.
Exports: No pub Needed
This language has no visibility mechanism. No pub, no private, no export.
Every top-level binding in a module is visible to any code that can write the module's path:
// src/helper/mod.yx
// plain has no modifier in front of it
plain: () => string = "Importable even without pub"use helper
// helper.plain() can be called directly, no pub neededIf you want to "restrict" a binding from being used externally, the only way is to not place it at the module's top level.
History of
pub: Earlier specs taught "usepubto declare exports, default to private." That design was overturned by RFC-029, and RFC-029g removed thepubkeyword entirely — it's now just an ordinary identifier, and writingpub x = 1will report an error for an undefined name. Top-level bindings are importable by default, regardless of any modifier. See Spec §3 for details.
Methods Must Be Explicitly Bound
Methods aren't auto-generated by pub; they are written explicitly on the type:
Point: Type = { x: Float, y: Float }
// Explicit method: first parameter is self
Point.distance: (self: &Point, other: &Point) -> Float = {
dx = self.x - other.x
dy = self.y - other.y
(dx * dx + dy * dy).sqrt()
}Troubleshooting
E5001: module not found
Check in order of likelihood:
- The directory entry is written as
index.yx— change it tomod.yx; this is the most common cause - Path is misspelled or at the wrong level —
use math.vectorcorresponds tosrc/math/vector.yx - Dependencies aren't installed — the error message will suggest running
yx install - The file is outside the project — the lookup starts only at two places: "the importer's directory" and "the project root"
E5003: export not found
The module was found, but it doesn't contain the name you want. Often it's a spelling difference — for example, in std.io it's read_line rather than read:
use std.io.{read_line} // ✅
use std.io.{read} // ❌ E5003Quick Reference
| Syntax | Effect |
|---|---|
use std.io | Namespace io, access via io.x |
use std.math.{sqrt} | Bare use of sqrt |
use std.io.{print as say} | Bare use of say |
use helper as h | Namespace h, access via h.x |
use std.io.{print} as say | Syntax error, use the inline form |
use .relative | Syntax error, path must be written in full from the module root |
pub fn foo | Semantically invalid, no visibility |
