Skip to content

Modules

As programs grow, you need ways to split code across files, avoid name collisions, and control what’s visible to the outside world. Zap’s module system is file-based and explicit.

import std.io // import the io module from the standard library
import std.collections // import the collections module
fn main() {
io.println("Hello from io!")
}
import std.io { println, read_line }
fn main() {
println("What's your name?")
let name = read_line()
}
import std.collections as col
let list = col.List.new()

Every .zap file is a module. The file name is the module name:

my_project/
├── main.zap
├── math.zap
└── utils/
├── strings.zap
└── files.zap
main.zap
import math
import utils.strings
fn main() {
print(math.add(2, 3))
print(strings.capitalize("hello"))
}

By default, all top-level declarations in a module are private. Use pub to make them accessible:

math.zap
pub fn add(a: Int, b: Int) -> Int {
a + b
}
fn internal_helper() {
// not visible outside this module
}

Directories create module hierarchies:

utils/strings.zap
pub fn capitalize(s: String) -> String {
// ...
}
// Access as: utils.strings.capitalize(...)
Convention Description
One module per file Keep modules focused and small
pub only what’s needed Minimise your public API surface
Group related modules Use directories for logical grouping
main.zap as entry point The compiler looks for main() here

Explore the Standard Library to see what modules ship with Zap.