Modules (roadmap #18a, #18b)
# main.tr
import "modules/geometry.tr" as geo # `as` is optional: the default name is the file's stem
import "modules/stats.tr"
fn main():
let shapes = [geo.Circle(1.0), geo.Rect(w: 2.0, h: 3.0)]
print(geo.total(shapes))
match shapes[0]:
geo.Circle(r) => print(r)
_ => print("not round")
# modules/geometry.tr
pub type Shape =
| Circle(r: Float)
| Rect(w: Float, h: Float)
fn square(x): x * x # private: only this file can use it
pub fn area(s): ...
- **
import "path.tr"at the top level, with the path relative to the importing file. Everything the file markspub** is available asname.thing: functions, types (geo.Shapein annotations), constructors (geo.Circle(1.0),geo.Rect(w: 2.0, h: 3.0), and in patterns), traits (impl geo.Describe for Int) and their methods (geo.describe(x)). - Visibility:
pubgoes beforefn,typeortrait. A public type's constructors and a public trait's methods are public with it. Impls are always in effect. Using a private name is an error that says where to addpub. - Inside a module, its own names are used unqualified; it can import other modules.
- Each file is loaded once, however many files import it. Import cycles are an error that shows the cycle.
- Errors point into the file they're in, also for runtime errors in native code, and in
--jsonoutput. - The run cache includes every imported file: changing a module rebuilds.
- Printed values show constructors without the module (
Circle(1.0)); types in messages andtrine checkare qualified (geometry.Shape).
How it works: each module's definitions are renamed to <module>.<name> and merged into one program before type checking, so the checker, all three engines and the tools work unchanged. A module's lines are numbered from k × 1,000,000 internally so every position stays unique and maps back to its file.
Local packages (35a)
A bare name imports a package listed in the nearest trine.toml (searched upward from the importing file):
# app/trine.toml
[package]
name = "app"
[dependencies]
geometry = { path = "../geometry" } # ../geometry/lib.tr (or a path ending in .tr)
import "geometry" as geo
Git dependencies, trine.lock and the shared build cache: see PACKAGES.md.
Capabilities per module (47a)
trine caps main.tr ends with one line per imported module: either "can't reach the outside world" or which of its functions take a Sys, a Dir or a function. A module has no authority of its own (there are no globals): it can use only what its importer passes to those functions. So reviewing a dependency's reach means reading those lines, not its code.
The store and the tools across modules (18c, 18d)
- The store (
.trine/, see HASHING.md) keys a module's definitions by file:modules/geometry.tr:area,../geometry/lib.tr:area,shapes@v1/lib.tr:area(a git package); definitions in the main file keep their plain names, and the library's are left out.trine storereports changes and renames per module, and a call that uses an old name after a rename says so: `modulegeo(modules/geometry.tr) has nototal—totalwas renamed tosum_area: same definition (hash 6d93c61c9bab), sogeo.sum_areahere can be replaced safely`. - **
trine fmt main.tr** formats the file and every module it imports from the same project (the directory of the nearesttrine.toml, or the main file's): not packages.--checklists every unformatted file. - **
trine lint** warns in the project's modules too (unused variables, private functions never called), not in packages or the library. - **
trine doc** documents the main file, then each project module's public definitions. - **
trine caps** covers every function, modules and packages included, and ends with a line per module (47a).