Modules
Learning Echo from scratch?
Start with Lesson 8 — Organizing programs. This page is the fuller module write-up.
Split a program across .echo files with export and import. Each file is its own module scope. Full contract: Module Semantics.
Basic Example
Two files in the same directory:
echo
# math.echo
export fn add(a: int, b: int) -> int {
return a + b;
}echo
# app.echo
import add from "math";
say(add(2, 3));Run the entry file (from the directory that contains both files):
bash
elang app.echoOutput
text
5How It Works
- Specifiers resolve relative to the importing file:
"math","./math","lib/math","../shared". - Reserved
std/…imports load from the Echo install tree (available after pip/pipx install):
echo
import stdOk from "std/meta";
say(stdOk());Output
text
true- Only
exported names are visible to importers. Private names stay in the defining module. importis not the same asuse/use mut(those are function-scope capture / mutation).- Nested demo in the repo: from a clone at the repo root,
elang examples/modules_demo/app.echoprints:
text
2+3 = 5
6^2 = 36
std ready: trueCommon Mistakes
- Expecting
math.add(...)dotted namespaces — not supported; bind names withimport. - Pointing
elang examples/...at a pipx-only install —examples/is clone-only. - Forgetting
exporton the name you import.
Related
- Module Semantics — full path / cycle / std rules
- Mini Programs
- Std Inventory
- CLI and Execution Model
