Modules & Imports¶
MOL has a module system for organizing code across files.
Importing Files¶
Use the use statement to import from other .mol files:
-- Import everything from a file
use "./utils.mol"
-- Import specific names
use "./math_helpers.mol" : add, multiply
-- Import with a namespace alias
use "./config.mol" as Config
show Config.api_key
Exporting¶
By default, all top-level names in a file are exported. Use export for explicit control:
-- helpers.mol
-- Only these two will be importable
export greet, farewell
define greet(name)
return "Hello, " + name
end
define farewell(name)
return "Goodbye, " + name
end
-- This is private — not exported
define internal_helper()
return "you can't import me"
end
Package Imports¶
Import built-in packages by name (without ./):
Available Packages¶
| Package | Contents |
|---|---|
std | Core utilities |
math | Extended math & statistics |
text | String processing |
collections | Stack, Queue, LinkedList |
crypto | Hashing, encoding |
random | Random number generation |
rag | RAG pipeline helpers |
Package Manager¶
Initialize a Project¶
Creates a mol.pkg.json manifest:
Install Packages¶
List Installed¶
Search the Registry¶
Module Patterns¶
Config Module¶
-- config.mol
export api_url, max_retries, timeout
let api_url be "https://api.example.com"
let max_retries be 3
let timeout be 5000
-- main.mol
use "./config.mol" : api_url, timeout
show f"Connecting to {api_url} (timeout: {timeout}ms)"
Utility Library¶
-- string_utils.mol
export capitalize, slugify, truncate
define capitalize(text)
let first be upper(slice(text, 0, 1))
let rest be lower(slice(text, 1, len(text)))
return first + rest
end
define slugify(text)
return text |> lower |> replace(" ", "-")
end
define truncate(text, max_len)
if len(text) <= max_len then
return text
end
return slice(text, 0, max_len) + "..."
end