Modules

Split code across files with import, and learn what a library may contain, what it exports and how import paths are resolved.

A first library

import "path" makes the functions and constants of another file available in your script. The imported file is called a library. Use a library when a script grows, or when two scripts need the same helper. This is a small library and a script that imports it:

lib/release.ok
# Helpers for release scripts.
let prefix = "v"

fn tag_name(version: string) {
  "{prefix}{version}"
}

fn bump(version: string, part: string) {
  let [major, minor, patch] = version.split(".").map(int)
  match part {
    "major" => "{major + 1}.0.0"
    "minor" => "{major}.{minor + 1}.0"
    _ => "{major}.{minor}.{patch + 1}"
  }
}
release.ok
import "./lib/release.ok"

let next = bump("1.4.2", "minor")
print(next, tag_name(next))
$ ok release.ok
1.5.0 v1.5.0

import without anything after the path brings every exported name of the file into scope, so bump and tag_name can be called directly.

Importing under a name

import "path" as name puts the exported names in a namespace called name. You call a function as name.function(...):

lib/release.ok
let prefix = "v"

fn tag_name(version: string) {
  "{prefix}{version}"
}
tag.ok
import "./lib/release.ok" as release

print(release.tag_name("2.0.0"))
print(release.prefix)
$ ok tag.ok
v2.0.0
v

In a long script a namespace is easier to read, because every call says which library the function comes from. The names in a namespace also cannot clash with your own names. Use import without as for a small set of helpers that you use everywhere, and as for every other library.

A namespace is a record of the exported names. A misspelt name gets an error that names the namespace, with a suggestion:

typo.ok
import "./lib/release.ok" as release

print(release.tagname("2.0.0"))
$ ok typo.ok
error[name]: `release` has no function `tagname`
 --> typo.ok:3:15
  |
3 | print(release.tagname("2.0.0"))
  |               ^^^^^^^ not exported by the library
  |
  = help: did you mean `release.tag_name`?

What a library exports

Every top-level fn and let of a library is exported, except names that start with _. Use a leading underscore for helpers that are not part of the library’s interface:

lib/names.ok
let _separator = "-"

fn _clean(word: string) {
  lower(trim(word))
}

fn slug(title: string) {
  title.split(" ").map(_clean).where(w => w != "") | join(_separator)
}
slug.ok
import "./lib/names.ok" as names

print(names.slug("  Hello  Big World "))
print(names)
$ ok slug.ok
hello-big-world
{slug: <fn slug>}

Functions in a library see the library’s own names, private ones included, and the names the library imports. They do not see the names of the script that imports the library, so a library behaves the same in every script that imports it.

Names a library imports are not exported again. If lib/math.ok imports lib/helpers.ok, a script that imports lib/math.ok does not get the helpers. A script that needs the helpers imports lib/helpers.ok itself.

Libraries only declare

A library may contain only fn declarations, constant let declarations and import. Loading a library never runs code. The order of your imports therefore does not matter, and importing a file cannot print, write files or start programs.

A constant is built from literals, lists and records, operators, string interpolation, other constants, path(...) and os.name, os.arch and os.exe. Any other computation belongs inside a function:

lib/config.ok
let settings = read("settings.toml")
app.ok
import "./lib/config.ok"
$ ok app.ok
error[syntax]: a library's top-level `let` must be a constant
 --> lib/config.ok:1:16
  |
1 | let settings = read("settings.toml")
  |                ^^^^^^^^^^^^^^^^^^^^^
  |
  = help: libraries never run code when imported; compute the value inside a function instead

The fix is a function the script calls when it needs the value:

fn settings() {
  read("settings.toml")
}

Statements such as print(...) or run at the top level of a library are errors for the same reason. A library may still declare fn main. ok calls main only when the file itself is run as a script.

Import paths

The path after import is a string literal, relative to the directory of the file that contains the import, not to your working directory. A library that imports the files next to it therefore works from any working directory. "./lib/release.ok" and "lib/release.ok" mean the same thing. The ./ shows the reader that the path is relative. .. goes up a directory, and paths use / on every OS.

ok refuses some paths, because they would make a script depend on the machine it runs on or on the network:

  • Absolute paths, such as "/opt/lib.ok", are an error that suggests a relative path.
  • URLs are an error: ok has no remote imports, package manager or search path.
  • The path must be written out as a string. import some_variable is a syntax error, so every import can be found without running anything.

A file that does not exist is reported at the import line:

missing.ok
import "./lib/nothing.ok"
$ ok missing.ok
error[io]: cannot import "./lib/nothing.ok": no such file
 --> missing.ok:1:8
  |
1 | import "./lib/nothing.ok"
  |        ^^^^^^^^^^^^^^^^^^

Clashes and cycles

An import without as puts names directly into your scope, so they must not clash with your own declarations or with another import. A clash is an error when the script loads, with the fix in the help line:

lib/a.ok
fn greet() {
  "hello from a"
}
lib/b.ok
fn greet() {
  "hello from b"
}
both.ok
import "./lib/a.ok"
import "./lib/b.ok"

print(greet())
$ ok both.ok
error[name]: `greet` from lib/b.ok clashes with a name already declared here
 --> both.ok:2:8
  |
2 | import "./lib/b.ok"
  |        ^^^^^^^^^^^^
  |
  = help: import it under a name instead: import "./lib/b.ok" as lib

To fix a clash, import one or both files with as. Importing the same file twice is not a clash: the second import does nothing.

Each file is loaded once per run, however many files import it. Two libraries that import each other, directly or through others, are an import cycle, and the error shows the whole cycle:

lib/one.ok
import "./two.ok"

fn one() {
  1
}
lib/two.ok
import "./one.ok"

fn two() {
  2
}
cycle.ok
import "./lib/one.ok"
$ ok cycle.ok
error[name]: import cycle: lib/one.ok -> lib/two.ok -> lib/one.ok
 --> lib/two.ok:1:8
  |
1 | import "./one.ok"
  |        ^^^^^^^^^^

Move the shared part into a third file that both import.

Imports in a project

A project (see Tasks) has a root file, such as .ok/ok.ok, and can have more files in .ok/. These files use the same import, with two additions:

  • Every import must stay inside the project root, after following symlinks. A path that escapes with .. or a symlink is an error, so the code you trust for a project is the code in that project. See Trust.
  • Importing another project’s root file with as, such as import "../frontend/ok.ok" as fe in .ok/ok.ok, mounts that project’s tasks under a namespace. See Tasks.

Where to go next

Edit this page on GitHub