Modules and projects
Import the math module
A module keeps related functions under a name. Import it when your program needs those functions.
use math
print(math.abs(-5))
5
use makes math available in this file. The call selects its abs function.
Imports and exports
Use 'use' to import a built-in or local module. Export the declarations that callers should see.
Module resolution and project configuration
Start a local path with './' to resolve it next to the importer. A project file can add shared module directories.
use math loads a registered module and binds it as math. use math as m binds the same module as m. A quoted import requires an alias. Explicit relative paths begin with ./ or ../ and resolve from the importing file. An unqualified quoted path first looks next to the importer, then searches the module directories in order. Absolute paths are resolved directly. Supply explicit file extensions in paths.
Put reusable code in its own file when several parts of your program need it. Save this as greeting.atom.
export func hello(name):
return 'Hello, {name}!'
In the same directory, save this as app.atom.
use './greeting.atom' as greeting
print(greeting.hello('Ada'))
Run atom run app.atom.
Hello, Ada!
The export keyword makes hello available to the other file. The importer calls it through the module name after as.
export func, export const and export name = expression publish top-level bindings. Export captures the value at the point the declaration or assignment runs. Rebinding a mutable name later does not replace that export automatically. Mutating a collection exported by reference is visible through the namespace. Other bindings are private. A module's exports are read-only from Atom. A file module is cached by canonical filename per VM after initialization. Circular imports during initialization raise an error. A return inside an imported module ends initialization and the importer receives the exports.
Use repeated --module-path DIRECTORY flags and optionally --project FILE with run, build, emit-c and publish. CLI directories precede project directories. Explicit CLI paths are relative to the working directory. Project entries are relative to the configuration file. The file consists of blank lines, whole-line # comments and module_path = DIRECTORY entries. Directory values are unquoted. For a project.conf alongside an app.atom, this makes lib/report.atom available as use 'report.atom' as report.
# Project-relative lookup
module_path = lib
See Multi-file application for a runnable example. The packagers walk the bytecode import declarations, recursively include file modules and embed named resources supplied by --resource NAME=FILE. An embedded application retrieves them through resource.read. Dynamic libraries loaded with ffi.load or ffi.extension have platform loader rules and are distributed as separate dependencies. An embedding host supplies its own source and resource loaders.