16. Modules and packages
A program of a few hundred lines fits in one file. Beyond that, you split it: one file for the data model, one for storage, one for the user interface. And you reuse code other people wrote: the standard library, and packages from git.
In this chapter you will learn:
- how
importbrings in other files and the standard library, - namespaces, which keep names from different parts of a program apart,
use, for shorter names,- projects with a
grav.toml, and where imports are looked for, - packages: adding, installing and updating dependencies.
Splitting a program into files
import "path" makes another file's declarations available. Here a small library and a program that uses it:
// file: util/mathx.grav
namespace mathx {
fn square(n: int) -> int { return n * n }
fn clamp(v: int, lo: int, hi: int) -> int { return v < lo ? lo : (v > hi ? hi : v) }
}// file: app.grav
import "util/mathx"
fn main() {
print(mathx.square(7))
print(mathx.clamp(150, 0, 100))
}- Paths are relative to the importing file. The
.gravextension can be left off. - A file is loaded once, however many files import it, so repeated and circular imports are harmless.
- A file without
mainis a library. Only the file you compile needsmain.
Namespaces
If two files both declare a parse function, which one does a call mean? Namespaces avoid the question. namespace name { ... } puts declarations under a name, and outside code refers to them as name.member:
namespace geometry {
class Circle {
public r: float
constructor(r: float) { self.r = r }
fn area() -> float { return 3.14159 * self.r * self.r }
}
namespace units {
fn cm(x: float) -> string { return "${x} cm" }
}
fn describe(c: Circle) -> string {
return "circle of " + units.cm(c.r) // own names need no prefix inside
}
}
fn main() {
let c = new geometry.Circle(2.0)
print(geometry.describe(c))
print(geometry.units.cm(c.area()))
}circle of 2 cm
12.56636 cmNamespaces nest (geometry.units.cm) and can be spread over several files.
use: shorter names
use makes names from a namespace available without the prefix. It has three forms:
namespace shapes {
class Circle {
public r: int
constructor(r: int) { self.r = r }
}
fn area(c: Circle) -> int { return 3 * c.r * c.r }
}
namespace legacy {
class Circle {
public d: int
constructor(d: int) { self.d = d }
}
}
use shapes::* // every name in shapes
use legacy.Circle as OldCircle // one name, renamed
use shapes as sh // a shorter name for the namespace
fn main() {
print(area(new Circle(2)))
print(new OldCircle(9).d)
print(sh.area(new sh.Circle(1)))
}12
9
3:: and . mean the same in use. A use applies inside the namespace it is written in (at the top of a file, to the whole file). When a name could mean two things, the closer declaration wins: a namespace's own names beat names brought in with use.
The standard library
import "std" loads the standard library. It is organised in categories, each in its own namespace, which you can also import on their own:
| Import | Contents |
|---|---|
import "std" | everything except networking |
import "std/core" | exceptions, Math, Random, Result/Option, Optional, Panic, GC |
import "std/collections" | Array, HashMap, HashSet, Deque, Stack, Queue, SortedMap, ... |
import "std/text" | Str, StringBuilder, Format |
import "std/data" | Json, Bytes |
import "std/io" | IO, File, Dir, Path, Term |
import "std/sys" | OS, Process, Command, Time, DateTime |
import "std/concurrency" | Mutex, AtomicInt, Channel, Thread, WaitGroup, ... |
import "std/net" | TCP, HTTP and WebSockets |
A single module can be imported too: import "std/collections/hashmap". The standard library reference has a page for every module.
Importing makes the module's names usable without a prefix. Three import forms let you choose what you get:
// file: forms.grav
import "std/text" // every name
import { HashMap, Array as Vec } from "std/collections" // only these, Array renamed
import "std/data" as data // behind a prefix: data.JsonBecause the library lives in namespaces, your names never clash with it. If your program declares its own Str, your code means yours, the library keeps using its own, and the library's is still reachable by its full name:
import "std"
class Str {
static fn length(s: string) -> int { return -1 }
}
fn main() {
print(Str.length("abc")) // ours
print(std.text.Str.length("abc")) // the library's
}-1
3Projects
A project is a folder with a grav.toml file. Create one with:
grav new inventory
cd inventory
grav runThat makes:
inventory/
├── .gitignore
├── grav.toml
└── src/main.gravgrav.toml describes the project:
name = "inventory"
main = "src/main.grav" # used by grav run, grav build and grav test without a file
paths = ["src", "vendor"] # extra places to look for imports
[dependencies]
colors = "github.com/someone/grav-colors@v1.2.0"
http2 = { git = "https://gitlab.com/team/http2", version = "v0.3.1" }Inside a project, grav run, grav build and grav test work without naming a file. The compiler finds grav.toml by looking in the current folder and its parents.
Where imports are found
import "x" is looked for in this order, and the error lists every place tried when nothing matches:
- relative to the importing file;
stdandstd/...in the standard library;- a dependency named
xingrav.toml(import "colors",import "colors/sub/file"); - each folder in
grav.toml'spaths; - each folder in the
GRAV_PATHenvironment variable (separated by:); - the standard library folder itself.
Packages
A package is a git repository of Grav code, on GitHub, GitLab, any git host, or a local path. The grav command manages them:
grav add github.com/someone/grav-colors@v1.2.0 # add a dependency and install it
grav add gitlab.com/team/http2 # no version: the newest tag
grav install # install everything grav.lock lists
grav update # move every dependency to its newest tag
grav update colors # just one
grav remove colors
grav packages # list dependencies and where they are- Versions are git tags. Without tags, the default branch is used, pinned to its current commit.
grav.lockrecords the exact commit of every dependency, including the dependencies of dependencies. Commit it to version control:grav installthen gives everyone the same code.- The cache keeps one copy of each version in
~/.local/grav/packages(setGRAV_PACKAGESto move it), shared by all your projects. - Importing a package by name (
import "colors") loads its entry file: thelibnamed in its owngrav.toml, or elsecolors.grav,lib.gravorsrc/lib.grav. The names in the package's namespaces are then usable without a prefix, as with the standard library (your own declarations win a clash). - Local packages:
grav add ../grav-dbuses a package straight from a folder on your machine, before it is published. It writesgrav-db = { path = "../grav-db" }tograv.toml: the folder is read in place, so edits show at once,grav installinstalls the package's own dependencies, and nothing is pinned ingrav.lock. Publish it, then switch withgrav add github.com/you/grav-db. (grav add --git <folder>clones a local repository's commits instead.) - In the editor, VS Code and the language server (Neovim, JetBrains) find the
grav.tomlabove the open file and follow its dependencies the way the compiler does: completion, hover and go to definition reach into the cached copy pinned bygrav.lock, or into a local package's folder. They never fetch: a package that is not installed yet is reported bygrav checkuntil you rungrav install.
To publish a package, push a repository with a grav.toml (with a lib = "..." entry and a grav = ">=1.7.0" requirement: the compiler versions it builds with) and tag a version: tools/release.sh 1.0.0 path/to/package checks it, tags it with its changelog entry and creates the GitHub release. See Versions and releases.
export and decorators
export marks a declaration as part of a module's public interface, and decorators like @Deprecated("...") attach notes to declarations. export is documentation: every top-level declaration is visible to importers. Decorators do nothing by themselves, but on classes and their members they are recorded with their arguments, and code reads them at run time through reflection (@Table("users"), @Column("email")).
@Deprecated("use total()")
export fn sum(a: int, b: int) -> int { return a + b }
fn main() {
print(sum(2, 3))
}5Summary
import "path"loads another file once;import "std"loads the standard library.namespacegroups declarations;useshortens names;import { A as B } fromandimport "x" as aliascontrol what an import brings in.grav newcreates a project with agrav.tomlthat names the entry point, import paths and dependencies.grav add,install,updateandremovemanage git-based packages, pinned bygrav.lock.
Exercises
- Split a program into
model.grav(aTaskclass),store.grav(functions that keep tasks in anArray) andmain.grav. - Put
model.grav's class innamespace todoand use it frommain.gravboth with the prefix and withuse todo::*. - Create a project with
grav new, move your files intosrc/, and run it with plaingrav run. - Import only
HashMapfromstd/collectionsand check thatArrayis now unknown. Read the hint in the error.
Next: C interop →