Projects and modules
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:
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:
| Option | Effect |
|---|---|
--runtime core|std | choose the runtime. The default is std. |
--no-git, --vcs none | do 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:
Write actus new app --runtime core instead.
Actus.toml
Actus.toml describes the project. This is the file that actus new writes:
The [package] part says what the project is:
| Field | Meaning |
|---|---|
name | the name of the package. Use letters, digits, - and _. |
version | the version of the package. |
edition | the language edition. alpha is the only one. |
entry | the verb where the program starts. |
source_root | the 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:
| Field | Meaning |
|---|---|
target | host builds for the computer you use. |
profile | debug, release or size. The command line can override it. |
runtime | std or core. See the runtime. |
backend | llvm (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:
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:
Source layout
A new project has one source file:
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:
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:
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:
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:
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:
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:
If a facade opens a file that is not there, the facade is src/tools/tools.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:
Add src/geometry/helpers.act:
And add a verb to src/geometry/rect.act that uses it:
perimeter is public and calls doubled. Run both from main.act:
But main.act cannot call doubled itself, because the facade opened it for
the module only. This main.act tries:
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:
The module b imports a with a plain import and then calls seven with no
name in front of it:
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():
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:
The import in b is for b. The module b::c writes its own import. Add it
to src/b/c/inner.act:
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:
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 whatactus newwrites. Everystd::module can be imported.runtime = "core"leaves the standard library out. This is for small programs that must not depend on an operating system.printlnstill works. Create such a project withactus new app --runtime core. ItsActus.tomlhas noruntimeline.
If you import a standard module in a core project, the compiler says what to
change:
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:
ops.act is the facade and contains open basic;. basic.act has one public
verb:
In the Actus.toml of the project that wants to use it, add a
[dependencies] part with the path:
Then import the module as mathkit::ops:
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:
After the mathkit dependency is added, it also lists the package with a
checksum of its files:
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:
Run actus lock to bring the file up to date:
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:
Do not edit Actus.lock by hand. A broken file is an error, and actus lock
writes a correct one.
What you learned
Actus.tomlhas[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 sayopentoo, to be visible outside. import name;gives youname::verb(...).open unitkeeps a file inside the module.std::modules needruntime = "std".[dependencies]with apathuses a package on your disk.actus lockupdatesActus.lockwhen it is out of date.
Next: Testing.