Skip to content

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 ​

yaoxiang
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 ​

yaoxiang
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:

yaoxiang
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:

yaoxiang
use std.io.{print as say}

main = () => {
    say("Using say instead of print")
}

Aliases Must Go Inside the Curly Braces This form is supported:

yaoxiang
use std.io.{print as say}   // ✅

The one below is rejected — the compiler will suggest the inline form:

yaoxiang
use std.io.{print} as say   // ❌ Positional alias, not supported

4. Module Alias ​

yaoxiang
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):

yaoxiang
use std.math as m   // ❌ Calling m.sqrt(...) reports E8001

Workaround: Use inline item aliases instead — they work fine for standard library modules:

yaoxiang
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
yaoxiang
// src/helper/mod.yx
use std.io

shout: (msg: string) -> Void = {
    io.print(msg)
}

label: () -> string = "src/helper/mod.yx"
yaoxiang
// 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:

yaoxiang
// src/helper/index.yx
label: () => string = "This will fail"
yaoxiang
use helper   // ❌ E5001: module 'helper' not found

Reason: 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:

yaoxiang
// src/helper/mod.yx
// plain has no modifier in front of it
plain: () => string = "Importable even without pub"
yaoxiang
use helper
// helper.plain() can be called directly, no pub needed

If 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 "use pub to declare exports, default to private." That design was overturned by RFC-029, and RFC-029g removed the pub keyword entirely — it's now just an ordinary identifier, and writing pub x = 1 will 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:

yaoxiang
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:

  1. The directory entry is written as index.yx — change it to mod.yx; this is the most common cause
  2. Path is misspelled or at the wrong level — use math.vector corresponds to src/math/vector.yx
  3. Dependencies aren't installed — the error message will suggest running yx install
  4. 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:

yaoxiang
use std.io.{read_line}   // ✅
use std.io.{read}        // ❌ E5003

Quick Reference ​

SyntaxEffect
use std.ioNamespace 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 hNamespace h, access via h.x
use std.io.{print} as saySyntax error, use the inline form
use .relativeSyntax error, path must be written in full from the module root
pub fn fooSemantically invalid, no visibility