Projects and modules

View source

What is in Actus.toml, how to split a program into modules with import and open, how to use the standard library and other packages, and what Actus.lock is for.

A project is a folder with an Actus.toml file and a src folder. This page explains the file, shows how to put code in more than one file, and shows where the standard library and other packages come in.

Create a project

You already know actus new. It makes a new folder:

bash
actus new shapes
text
Initialized empty Git repository in /home/you/shapes/.git/
created `shapes`

Next steps:
  cd shapes
  actus check
  actus build --emit exe
  actus build --release --emit exe
  actus run

If the folder already exists, go into it and run actus init. It adds Actus.toml, Actus.lock and src/main.act and leaves your other files alone.

Both commands take the same options:

OptionEffect
--runtime core|stdchoose the runtime. The default is std.
--no-git, --vcs nonedo not start a Git repository.

Put the name of the project first. actus new --runtime core app fails, because the first argument must be the name:

text
error: unknown option `--runtime` for `actus new`

Write actus new app --runtime core instead.

Actus.toml

Actus.toml describes the project. This is the file that actus new writes:

toml
[package]
name = "shapes"
version = "0.1.0"
edition = "alpha"
entry = "main"

[build]
target = "host"
profile = "debug"
runtime = "std"

The [package] part says what the project is:

FieldMeaning
namethe name of the package. Use letters, digits, - and _.
versionthe version of the package.
editionthe language edition. alpha is the only one.
entrythe verb where the program starts.
source_rootthe folder with your code. The default is src.
source_limits"limitless" turns off the size limits of --strict. See Everyday commands.

The [build] part says how to build it:

FieldMeaning
targethost builds for the computer you use.
profiledebug, release or size. The command line can override it.
runtimestd or core. See the runtime.
backendllvm (the default) or cranelift.

There is also a [dependencies] part, which is explained in Use another package.

Actus checks the file before it does anything else. A field it does not know is an error, and so is a value it does not accept:

toml
[package]
name = "shapes"
version = "0.1.0"
edition = "alpha"
entry = "main"
foo = 1
bash
actus check
text
/home/you/shapes/Actus.toml: error[E1800] at 6:1: cannot parse the manifest: unknown field `foo`, expected one of `name`, `version`, `edition`, `entry`, `source_root`, `source_limits`, `dependencies`

The message names the file, and the line and column of the key that is wrong.

The same happens for a profile that does not exist. The error points at the value and names the values that are allowed:

text
/home/you/shapes/Actus.toml: error[E1800] at 9:11: cannot parse the manifest: unknown variant `fast`, expected one of `debug`, `release`, `size` in `build.profile`

Source layout

A new project has one source file:

text
shapes/
├── .gitignore
├── Actus.lock
├── Actus.toml
└── src/
    └── main.act

actus new writes Actus.lock with the project. The capsula folder appears when you build. Both are listed in the .gitignore that actus new writes, so Git leaves them out.

src/main.act has the entry verb main. Code that you want to import goes in a folder of its own inside src, as the next section shows. A single file next to main.act is not a module that you can import.

Your own modules

A module is a folder in src with code in it. To use a module from another file, you import it. A module has one facade: a file with the same name as the folder. The facade decides what other code may use.

Make a module called geometry. Create the folder src/geometry with two files. The first is the facade, src/geometry/geometry.act:

actus
open rect;

open rect; says that the file rect.act in the same folder is part of the module and that its public verbs can be used from outside. The second file is src/geometry/rect.act:

actus
open verb area(abs width: Int, abs height: Int) -> Int {
    return width * height;
}

verb describe() {
    println(string: "a rectangle");
}

The word open in front of verb makes area public. describe has no open, so it stays private to the module. Now use the module in src/main.act:

actus
import geometry;

verb main() {
    erg width = 6;
    erg height = 7;
    erg result = geometry::area(width: abs width, height: abs height);
    println(int: result);
}
bash
actus run
text
42
process exited with status 0

import geometry; makes the name geometry available. You reach the verbs of the module with geometry::, as in geometry::area(...).

Two things must both be true for an outside file to use a verb: the verb says open, and the facade opens its file. If you leave out one of them, the verb is not visible. Calling the private describe fails:

actus
import geometry;

verb main() {
    geometry::describe();
}
bash
actus check
text
/home/you/shapes/src/main.act: error[E1109] at 4:5: private declaration `describe` from module `geometry` is not exported by facade `/home/you/shapes/src/geometry/geometry.act`; help: mark `describe` as `open` in its module to export it

The error says that describe is private and that the facade does not export it. The help line names the fix: put open in front of the verb. Files inside the same module can use each other's private verbs freely.

Common module errors

Forgetting the import gives an error too. Here main.act is the first program of this section without its import geometry; line:

actus
verb main() {
    erg width = 6;
    erg height = 7;
    erg result = geometry::area(width: abs width, height: abs height);
    println(int: result);
}
bash
actus check
text
/home/you/shapes/src/main.act: error[E1069] at 4:18: unknown verb `geometry::area`

If you import a folder that has no facade, or a name that is not a module at all, the compiler tells you which file it expected:

actus
import missing;

verb main() {
    println(string: "never runs");
}
bash
actus check
text
/home/you/shapes/src/main.act: error[E1101]: module `missing` is missing its canonical facade `/home/you/shapes/src/missing/missing.act`

If a facade opens a file that is not there, the facade is src/tools/tools.act:

actus
open gone;
actus
import tools;

verb main() {
    println(string: "never runs");
}
bash
actus check
text
/home/you/shapes/src/tools/tools.act: error[E1104] at 1:1: module `tools` facade `/home/you/shapes/src/tools/tools.act` references unknown sibling `gone.act`

Keep a helper inside the module

Sometimes a verb should be shared by the files of a module but not by the rest of the program. Put it in its own file and open that file with open unit. Change src/geometry/geometry.act to this:

actus
open rect;

open unit helpers;

Add src/geometry/helpers.act:

actus
open verb doubled(abs value: Int) -> Int {
    return value * 2;
}

And add a verb to src/geometry/rect.act that uses it:

actus
open verb area(abs width: Int, abs height: Int) -> Int {
    return width * height;
}

open verb perimeter(abs width: Int, abs height: Int) -> Int {
    return doubled(value: abs (width + height));
}

perimeter is public and calls doubled. Run both from main.act:

actus
import geometry;

verb main() {
    erg width = 6;
    erg height = 7;
    println(int: geometry::area(width: abs width, height: abs height));
    println(int: geometry::perimeter(width: abs width, height: abs height));
}
bash
actus run
text
42
26
process exited with status 0

But main.act cannot call doubled itself, because the facade opened it for the module only. This main.act tries:

actus
import geometry;

verb main() {
    erg side = 5;
    println(int: geometry::doubled(value: abs side));
}
bash
actus check
text
/home/you/shapes/src/main.act: error[E1109] at 5:18: private declaration `doubled` from module `geometry` is not exported by facade `/home/you/shapes/src/geometry/geometry.act`; help: `doubled` is already `open`, but the facade exports `helpers` only to its unit; to export it, change `open unit helpers;` to `open helpers;` in `/home/you/shapes/src/geometry/geometry.act`

The help line names the real cause. doubled already has open, but the facade opened its file with open unit, which is for the module only. If other code should see doubled, change the facade to open helpers;, as the help says. Use plain open for what other code should see. Use open unit for what only the module should see.

The help line changes with the cause. A verb without open gets the help you saw for describe. A verb with open in a file that the facade does not list at all gets a third line. It says that the facade does not export the file and tells you to add open helpers; to the facade, with the path of the facade.

Imports belong to one module

An import works in the module whose file writes it. Two rules follow from that, and both give the error E1119 when they are broken.

A plain import opens nothing. import { a }; gives the file the name a, and nothing else. The verbs of a are not in scope without their name. Say which module a verb comes from with a::seven(). Or ask for the verbs explicitly with import { open a };, which brings every public verb of a into scope. A selecting import, import { a { seven } };, brings only the names you list.

Here the module a has one public verb, seven:

actus
open seven;
actus
open verb seven() -> Int {
    return 7;
}

The module b imports a with a plain import and then calls seven with no name in front of it:

actus
open via;
actus
import { a };

open verb via_b() -> Int {
    return seven();
}
actus
import b;

verb main() {
    println(int: b::via_b());
}
bash
actus check
text
/home/you/shapes/src/b/via.act: error[E1119] at 4:12: `seven` is exported by module `a`, which module `b` imports without `open`; a plain import binds only the namespace `a`, so write `a::seven` or import it with `import { open a }`

The message names the verb, the module that exports it, and the two ways out. The first one is the better one, because the reader of b sees where seven comes from. Change the call to a::seven():

actus
import { a };

open verb via_b() -> Int {
    return a::seven();
}
actus
import b;

verb main() {
    println(int: b::via_b());
}
bash
actus run
text
7
process exited with status 0

Imports are not inherited. A module inside another module is a folder inside its folder. The inner module does not see the imports of the outer one. Here the module b imports a and has a child module c that uses a too:

actus
open seven;
actus
open verb seven() -> Int {
    return 7;
}
actus
import { a };

open c;

open verb outer() -> Int {
    return a::seven();
}
actus
open inner;
actus
open verb deep() -> Int {
    return a::seven();
}
actus
import b;

verb main() {
    println(int: b::outer());
    println(int: b::deep());
}
bash
actus check
text
/home/you/shapes/src/b/c/inner.act: error[E1119] at 2:12: `a::seven` is imported by module `b`, not by `b::c`; imports are not inherited, so add the import to a file of `b::c`: `import { a }` and qualify the name with `a::`, or `import { open a }`

The import in b is for b. The module b::c writes its own import. Add it to src/b/c/inner.act:

actus
import { a };

open verb deep() -> Int {
    return a::seven();
}
actus
import b;

verb main() {
    println(int: b::outer());
    println(int: b::deep());
}
bash
actus run
text
7
7
process exited with status 0

The same holds for the standard library. A child module that calls a std::string verb needs its own import std::string;, even when its parent has one. The parent facade opens c, so main reaches deep as b::deep().

The standard library

The standard library is a set of modules whose names start with std::. You import them in the same way:

actus
import {
    std::string,
    geometry,
};

verb main() {
    erg width = 6;
    erg height = 7;
    erg word = "area";
    erg length = string::string_length(text: abs word);
    case dat length {
        Result.Ok(count) => println(int: count),
        Result.Err(_) => println(string: "cannot measure"),
    };
    println(int: geometry::area(width: abs width, height: abs height));
}
bash
actus run
text
4
42
process exited with status 0

When you import more than one module, actus fmt puts them in one block, as above. Inside the block, each name is a module path. std::string becomes string.

string::string_length returns the length of a String in bytes. It can fail, so it returns a Result, and the program takes it apart with case, as in Errors: Result and Option. The console calls, such as println, are part of the language and need no import.

Among the standard modules of Actus 0.3.0 are std::io, std::fs, std::path, std::process, std::string and std::time. They need runtime = "std". The page Printing and input/output shows how to read a line with std::io.

The runtime

The runtime field in [build] decides whether the standard library is part of the build:

  • runtime = "std" is what actus new writes. Every std:: module can be imported.
  • runtime = "core" leaves the standard library out. This is for small programs that must not depend on an operating system. println still works. Create such a project with actus new app --runtime core. Its Actus.toml has no runtime line.

If you import a standard module in a core project, the compiler says what to change:

text
/home/you/app/src/main.act: error[E1100] at 1:1: module `std::io` is part of the standard library, which this build does not use; set `runtime = "std"` under `[build]` in Actus.toml

Use another package

A package can use code from another package that is on your disk. Say you have a second project, mathkit, next to shapes. A package puts its modules in a folder named after the package. So mathkit has these files:

text
mathkit/
├── Actus.toml
└── src/
    └── mathkit/
        └── ops/
            ├── basic.act
            └── ops.act

ops.act is the facade and contains open basic;. basic.act has one public verb:

actus
open verb triple(abs value: Int) -> Int {
    return value * 3;
}

In the Actus.toml of the project that wants to use it, add a [dependencies] part with the path:

toml
[dependencies]
mathkit = { path = "../mathkit" }

Then import the module as mathkit::ops:

actus
import mathkit::ops;

verb main() {
    erg value = 14;
    println(int: ops::triple(value: abs value));
}

The first actus check or actus run after you edit [dependencies] stops and asks you to update the lock file. The next section explains it.

Actus.lock

Actus.lock is a file that Actus writes for you. actus new and actus init write it with the project, and actus lock writes it again. The other commands only read it. It records what the build depends on: the runtime and every package in [dependencies]. For a new project it is short:

text
lockfile_version = 3
runtime = "std"
runtime_version = "0.1.0"
runtime_checksum = "5519105f540d259c"
package = []

After the mathkit dependency is added, it also lists the package with a checksum of its files:

text
lockfile_version = 3
runtime = "std"
runtime_version = "0.1.0"
runtime_checksum = "5519105f540d259c"

[[package]]
name = "mathkit"
version = "0.1.0"
source = "path"
path = "../mathkit"
checksum = "59848c64185c3065"

check, build, run, test and watch never write the file. When it is missing, or when it is there but no longer matches the project, they stop. This happens when you change [dependencies] or runtime, and when a dependency's files change. A missing file gives the same error code with the message Actus.lock is missing; run actus lock to create it.

An out of date file looks like this:

bash
actus check
text
/home/you/shapes/Actus.lock: error[E1800]: Actus.lock is out of date: it does not match the dependencies in Actus.toml; run `actus lock` to update it

Run actus lock to bring the file up to date:

bash
actus lock
text
updated `./Actus.lock`

To only check, without writing, use actus lock --check. It exits with status 1 when the file is missing or out of date. When it is up to date, it says so:

bash
actus lock --check
text
checked `./Actus.lock` successfully

Do not edit Actus.lock by hand. A broken file is an error, and actus lock writes a correct one.

What you learned

  • Actus.toml has [package], [build] and [dependencies]. Unknown fields and values are errors.
  • A module is a folder with a facade named like the folder. The facade opens files, and a verb must say open too, to be visible outside.
  • import name; gives you name::verb(...). open unit keeps a file inside the module.
  • std:: modules need runtime = "std".
  • [dependencies] with a path uses a package on your disk.
  • actus lock updates Actus.lock when it is out of date.

Next: Testing.