Skip to content

Modules

A program grows beyond one file by importing others. Each file is a module: its name is the file’s name, and it shows the others only what it chooses.

main.kova
import geometry
fn main {
let p := geometry.Point { x := 3.0, y := 4.0 }
print(geometry.length(p))
}
// geometry.kova, next to main.kova
public:
struct Point {
public:
x: Float
y: Float
}
fn length(p: Point) -> Float {
return (square(p.x) + square(p.y)).sqrt
}
private:
fn square(x: Float) -> Float {
return x * x
}
5.0

kova run main.kova finds geometry.kova by itself: there is no project file, no list of files to keep up to date. The names of a module are always written with it, geometry.Point, geometry.length, so a reader always knows where a name comes from.

In an imported file, everything is private unless written under public:: only that file can use it. private: hides again what follows. A struct’s fields follow the same rule inside the struct, so a type can show its methods and keep its fields to itself.

main.kova
import geometry
fn main {
print(geometry.square(2.0))
}
geometry.kova
fn square(x: Float) -> Float {
return x * x
}
error: `geometry.square` is private in geometry.kova
--> main.kova:5:11
|
5 | print(geometry.square(2.0))
| ^^^^^^^^^^^^^^^
= help: to use it from another file, write it under `public:` in geometry.kova

Private by default means a module’s inside can change without breaking the files that use it: only what is public is a promise. The compiler also refuses a public function that takes or gives a private type, since no one outside could use it.

  • import geometry as geo gives the module a shorter name: geo.Point.
  • A file in a folder is written with dots: import shapes.circle reads shapes/circle.kova, and calls it circle (circle.Circle).
  • Every import starts from the folder of the program’s main file, even inside shapes/circle.kova: import shapes.point, never a path that depends on where the importing file is.
  • Two files cannot import each other, directly or through others: the compiler shows the circle.

Some of the library is in modules that come with Kova. They need an import, but no file:

ModuleFor
filesreading and writing files, folders
systemenvironment variables, running other programs
webasking sites, and answering as a server
securesecure chance, password hashes, encryption
logdated lines on the error output, by level
uinative windows
import files
fn main {
print(files.exists("surely-not-here.txt"))
}
false

Without the import, the compiler says which one to write. What every program has without an import (print, random, texts, lists, dates, JSON…) is listed in the library.

A module has no main: it is not run on its own. kova test geometry.kova runs its tests, and the editor checks it like any file.

A package is a module someone else wrote, published as a git repository. The import names it by its address and an exact version:

import json from "github.com/ada/kova-json" version "1.2.0"
fn main {
print(json.pretty("[1, 2]"))
}

How it behaves:

  • The first kova run, kova build or kova test downloads it with git (which must be installed): the tag v1.2.0, or else 1.2.0, of https://github.com/ada/kova-json. It is kept in a cache every program shares (~/.cache/kova/packages on Linux, ~/Library/Caches/kova/packages on a Mac, %LOCALAPPDATA%\kova\packages on Windows; the variable KOVA_PACKAGES chooses another folder). After that, no network is needed.

  • kova.lock, written next to the program, notes each package’s commit and a SHA-256 of its files. If those files are ever different (the tag moved, the cache was edited), the program is refused:

    error: `github.com/ada/kova-json` 1.2.0 is not what it was when `kova.lock` noted it: its files changed. If that is expected, remove its line from `kova.lock`

    Put kova.lock in your own repository: whoever builds your program gets exactly your packages.

  • The editor never downloads: until the first kova run, it says the package is not downloaded yet.

  • A program uses one version of a package. Importing it twice with two versions is an error.

A package’s repository holds a package.kova: that is what import reads. Its other files are its own modules (import words inside it reads its words.kova), and like any module it shows only what is under public:.

kova search looks in the package index, a public list of packages, for every word you give it, and prints the import line to copy:

Terminal window
kova search json
json 1.2.0: Reads and writes JSON
import json from "github.com/ada/kova-json" version "1.2.0"

The number is the highest version the package’s repository has tagged, so it is always current. Offline, the search uses the last index it saw and says so.

The index only helps you find a package. import never reads it: your program depends on the address written in it and on kova.lock, whether the index changes or disappears.

To publish a package:

  1. Put a package.kova at the root of a public git repository.
  2. Tag a version: git tag v1.0.0, then git push --tags.
  3. It already works for anyone who writes its address in an import.
  4. To make it findable, add a line to index.txt in kova-packages by pull request: json github.com/ada/kova-json Reads and writes JSON.

New versions need nothing more than a new tag.