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.
import geometry
fn main { let p := geometry.Point { x := 3.0, y := 4.0 } print(geometry.length(p))}// geometry.kova, next to main.kovapublic:
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.0kova 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.
Public and private
Section titled “Public and private”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.
import geometry
fn main { print(geometry.square(2.0))}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.kovaPrivate 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.
Other names, other folders
Section titled “Other names, other folders”import geometry as geogives the module a shorter name:geo.Point.- A file in a folder is written with dots:
import shapes.circlereadsshapes/circle.kova, and calls itcircle(circle.Circle). - Every
importstarts from the folder of the program’s main file, even insideshapes/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.
Kova’s own modules
Section titled “Kova’s own modules”Some of the library is in modules that come with Kova. They need an import, but no file:
| Module | For |
|---|---|
files | reading and writing files, folders |
system | environment variables, running other programs |
web | asking sites, and answering as a server |
secure | secure chance, password hashes, encryption |
log | dated lines on the error output, by level |
ui | native windows |
import files
fn main { print(files.exists("surely-not-here.txt"))}falseWithout 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.
Tests and modules
Section titled “Tests and modules”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.
Packages
Section titled “Packages”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 buildorkova testdownloads it withgit(which must be installed): the tagv1.2.0, or else1.2.0, ofhttps://github.com/ada/kova-json. It is kept in a cache every program shares (~/.cache/kova/packageson Linux,~/Library/Caches/kova/packageson a Mac,%LOCALAPPDATA%\kova\packageson Windows; the variableKOVA_PACKAGESchooses 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.lockin 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:.
Finding and publishing a package
Section titled “Finding and publishing a package”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:
kova search jsonjson 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:
- Put a
package.kovaat the root of a public git repository. - Tag a version:
git tag v1.0.0, thengit push --tags. - It already works for anyone who writes its address in an
import. - To make it findable, add a line to
index.txtin kova-packages by pull request:json github.com/ada/kova-json Reads and writes JSON.
New versions need nothing more than a new tag.