Verbs

View source

Declare verbs, pass labelled arguments, return values with return, and give main an exit status.

A verb is Actus's word for a function: a named piece of code that you can run from other places. You have used one in every program so far. main is a verb. This page shows how to write your own.

Declare and call a verb

A verb declaration has the word verb, a name, a list of parameters in parentheses, an optional return type after ->, and a body in braces.

actus
verb main() {
    greet();
    erg first = 2;
    erg second = 3;
    erg total = add(left: abs first, right: abs second);
    println(int: total);
    println(int: add(left: abs total, right: abs 10));
}

verb greet() {
    println(string: "Hello!");
}

verb add(abs left: Int, abs right: Int) -> Int {
    return left + right;
}
bash
actus run
text
Hello!
5
15
process exited with status 0

There are two verbs besides main:

  • greet has no parameters and no return type. It does something and gives nothing back.
  • add has two parameters and returns an Int.

Each parameter has a role, a name and a type: abs left: Int. The role abs means that add only reads the value. The next sections explain how the role shows up in calls. A verb can be declared before or after the place that uses it, so main can come first.

Calls use labels and roles

When you call a verb, you write the name of each parameter, a colon and the argument: add(left: abs first, right: abs second). This is a labelled argument. The label makes a call readable, because you see what each value is for. The labels can come in any order:

actus
verb add(abs left: Int, abs right: Int) -> Int {
    return left + right;
}

verb main() {
    erg first = 2;
    erg second = 3;
    println(int: add(right: abs second, left: abs first));
}
bash
actus run
text
5
process exited with status 0

An argument also repeats the role of its parameter. The parameter says abs, and the call says abs first. Leave the role out and the compiler stops you:

actus
verb add(abs left: Int, abs right: Int) -> Int {
    return left + right;
}

verb main() {
    erg first = 2;
    erg second = 3;
    println(int: add(left: first, right: second));
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1016] at 8:28: argument does not satisfy role of `left` in `add`

The role at the call site is not decoration. It shows, right where the verb is called, how the value is used: here, only read. Ownership roles explains all four roles.

For an abs parameter you can also pass a number written in the call, as in abs 10 in the first example, or a calculation in parentheses, like abs (first + 1).

Using a label that the verb does not have is an error, and so is the wrong number of arguments:

actus
verb add(abs left: Int, abs right: Int) -> Int {
    return left + right;
}

verb main() {
    erg first = 2;
    erg second = 3;
    println(int: add(a: abs first, b: abs second));
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1012] at 8:29: unknown parameter `a` in call to `add`
actus
verb add(abs left: Int, abs right: Int) -> Int {
    return left + right;
}

verb main() {
    erg first = 2;
    println(int: add(left: abs first));
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1015] at 7:18: wrong argument count in `add`

Return a value

A verb that has a return type gives a value back with return. A return can be anywhere in the body, so you can leave early:

actus
verb sign(abs value: Int) -> Int {
    if value < 0 {
        return -1;
    }
    if value == 0 {
        return 0;
    }
    return 1;
}

verb pick(abs value: Int) -> Int {
    return if value > 10 {
        3
    } else {
        1
    };
}

verb say(abs value: Int) {
    if value == 0 {
        println(string: "zero");
        return;
    }
    println(string: "not zero");
}

verb main() {
    erg low = -4;
    erg high = 20;
    erg none = 0;
    println(int: sign(value: abs low));
    println(int: sign(value: abs none));
    println(int: pick(value: abs high));
    say(value: abs none);
    say(value: abs high);
}
bash
actus run
text
-1
0
3
zero
not zero
process exited with status 0

sign returns at the first if that matches. pick returns the value of an if, as you saw in Conditions and loops. say has no return type, so it uses return; without a value to stop early.

A value is returned only with return. If the last line of the body is an expression without return, the compiler explains what to do:

actus
verb add(abs left: Int, abs right: Int) -> Int {
    left + right
}

verb main() {
    erg first = 2;
    erg second = 3;
    println(int: add(left: abs first, right: abs second));
}
bash
actus check
text
/home/you/hello/src/main.act: error[E0003] at 3:1: expected `;` after this expression; a verb returns a value with `return`, as in `return <expression>;`

If the end of the body can be reached without a return, you get another error:

actus
verb add(abs left: Int, abs right: Int) -> Int {
    erg sum = left + right;
}

verb main() {
    erg first = 2;
    erg second = 3;
    println(int: add(left: abs first, right: abs second));
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1028] at 1:48: verb must return a value on every path

The return value has a type, and the type is checked where the value is used:

actus
verb square(abs n: Int) -> Int {
    return n * n;
}

verb main() {
    erg five = 5;
    erg text: String = square(n: abs five);
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1027] at 7:5: type mismatch for `text`: expected `String`, found `Int`

You do not have to use a returned value. Calling square(n: abs five); on a line of its own runs the verb and drops the result.

The other way round does not work. A verb without a return type, such as greet, gives nothing back, so its call cannot be used as a value:

actus
verb greet() {
    println(string: "Hello!");
}

verb main() {
    erg result = greet();
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1141] at 6:18: `greet` returns nothing, so its call has no value to bind or use; call it as a statement on its own

The same error appears when you pass such a call as an argument, use it in an expression or return it. Write greet(); on a line of its own instead.

A verb that acts on a value

Some verbs are about one value: bump a counter, show it. Name that parameter self. The call writes self like any other argument, with its role:

actus
struct Counter {
    count: Int,
}

verb main() {
    erg counter: Counter = Counter {
        count: 0,
    };
    bump(self: ins counter);
    bump(self: ins counter);
    show(self: abs counter);
}

verb bump(ins self: Counter) {
    self.count += 1;
}

verb show(abs self: Counter) {
    println(int: self.count);
}
bash
actus run
text
2
process exited with status 0

bump changes the counter, so main lends it with ins. show only reads it, so main shows it with abs. The call tells the reader what each verb does to the value, in the same way as for every other argument. There is one form for a call. A call such as counter.bump() is an error (E1152) and the message names the form to write instead:

actus
struct Counter {
    count: Int,
}

verb main() {
    erg counter: Counter = Counter {
        count: 0,
    };
    counter.bump();
}

verb bump(ins self: Counter) {
    self.count += 1;
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1152] at 9:5: `receiver.bump(...)` does not call a verb: a call names every participant with its role, so write `bump(self: ins receiver, ...)`

main and the exit status

The program starts at main. It has no parameters. It can have no return type, or it can return an Int, which becomes the exit status of the program. By convention 0 means success and any other number means that something went wrong.

actus
verb main() -> Int {
    println(string: "finishing with status 3");
    return 3;
}
bash
actus run
text
finishing with status 3
process exited with status 3

The last line comes from actus run. It reports the status of your program. A program that you built keeps the same status, and the shell shows it with echo $?:

bash
actus build --emit exe
text
built `/home/you/hello/capsula/debug/x86_64-unknown-linux-gnu/main.bin`
bash
./capsula/debug/x86_64-unknown-linux-gnu/main.bin; echo $?
text
finishing with status 3
3

Without a return type, main exits with status 0. Exit statuses are one byte on most systems, so use numbers from 0 to 255. A larger number is cut down: returning 300 gives 44.

What you learned

  • verb name(role label: Type, ...) -> Type { ... } declares a verb.
  • Calls name every argument, and each argument carries its role: add(left: abs first, right: abs second).
  • A parameter named self is called like any other: bump(self: ins counter).
  • A verb gives a value back only with return.
  • main is where the program starts. Its Int result is the exit status.

Next: Structs, enums and case.