Modules
As your projects grow, you can split your code across several files and share pieces between them with export and import.
Exporting from a file
Put export in front of a function or variable to make it available to other files. Anything without export stays private to its own file.
// math.blm
export let GOLDEN = 1.618
export fn double(x) {
return x * 2
}
// Private helper — not visible to other files.
fn secret() {
return 42
}
Importing into another file
Use import { ... } from "..." to bring exported names into another file. List the names you want inside the braces. The path is the other file, written in quotes — the .blm extension is optional.
// main.blm
import { double, GOLDEN } from "./math"
print(double(21)) // 42
print(GOLDEN) // 1.618
Paths starting with ./ are relative to the current file, so "./math" means "the file math.blm next to me".
Running it
Because modules read other files from disk, they run with the Bloom command-line tool rather than in the browser playground:
bloom run main.blm
This prints:
42
1.618
Rules to remember
importandexportmust be at the top level of a file — not inside a function or loop.- You can only import names that the other file actually
exports. Importing something that isn't exported is an error. - Two files cannot import each other (a circular import). If you need to share code between them, move it into a third file that both import.
- Each file is loaded once, even if several files import it.