Skip to content
Plumlang

Language reference · Section 9

ModulesStage 5

9.1 Files are modules

Every .plum file is a module. Its top-level statements run once, in order, the first time it is imported, or when it is the program being run. A program needs no main function. A module's functions exist before its first statement runs; its variables exist once their declaration has run (section 6.1).

9.2 seed

seed imports a module, by standard-library name or by path relative to the importing file:

seed math                       # standard library: math.sqrt(2.0)
seed net.http                   # use as http.server()
seed net.http as web            # use as web.server()
seed ./shapes                   # the file shapes.plum next to this one
seed ./models/user as user      # models/user.plum
seed math { sqrt, PI }          # bring names in directly: sqrt(PI)

A module's name is the last part of its path. Imports must come before other statements. Circular imports are a compile error that shows the cycle (E0311).

With seed ./shapes, the module's names are reached through it: shapes.area(c), and in types shapes.Circle. With seed ./shapes { Circle, area }, those names are used directly. Either way the names are the module's own: a variable it changes is changed for everyone who imported it. Its kinds' cases can be used bare in match patterns (Round =>).

Each file is loaded once, however many modules import it, and its top-level statements run once, before the statements of the first module that imports it. A file may be named like a keyword (seed ./models/plant).

9.3 Packages

A directory with a plum.toml is a package; its entry file is the program, so plum run my-project runs it.

[package]
name = "weather-api"
version = "0.1.0"
entry = "main.plum"
lib = "lib.plum"          # what `seed weather_api` imports (default: entry)

[dependencies]
colors = "^1.0"

Packages published on Orchard, Plumlang's registry, are added with plum add NAME and seeded by name. A name that isn't a standard module (nor under one, like net.x) is a package: seed colors imports the package's lib file, and seed colors.palette.dark its file palette/dark.plum. A - in a package's name is written _ in seed (seed weather_api for weather-api). The module's name is the last part, as for other modules.

A module may seed only the packages its own plum.toml lists under [dependencies] (E0908); which version is used comes from the project's plum.lock, and a declared package that isn't installed is E0909. The project is the nearest directory with a plum.toml above the file being run. Version ranges, the lock file and the commands are described in orchard.md.

9.4 Visibility

Every top-level name is visible to modules that import it, except names starting with _, which are private to their module. Fields and methods starting with _ are private to their tree (and the trees that extend it).