Ownership roles
What erg, abs, ins and dat mean, what the compiler refuses, and why the roles make data flow easy to read.
Every value in a program has to be somewhere, and someone has to be
responsible for it. Actus makes you write down who. The word you write is a
role. There are four of them: erg, abs, ins and dat.
| Role | Short name | What it means |
|---|---|---|
erg | owner | You own the value. You may read it, change it and hand it on. |
abs | view | You may only look at the value. The owner keeps it. |
ins | loan | You may change the value during this call. The owner gets it back. |
dat | transfer | The value is handed over for good. The old owner cannot use it again. A number or a Bool is copied. |
You have already seen all four. This page goes through them one at a time, with the mistakes the compiler catches for each one.
Roles appear in two places: where a variable or parameter is declared
(abs value: Int), and where a value is passed in a call
(show(value: abs score)). The declaration says what the verb will do. The
call says what the caller allows. The two must agree.
erg: you own it
erg makes an owner. A variable made with erg can be read, changed and
passed on. You have used it since
Variables and values.
A parameter can be erg too. The verb then gets a value of its own to use and
change. In the next program, add_one changes its own value, and for a
plain Int the caller's n is not touched:
The argument can also be a calculation, as in add_one(value: erg n + 1).
abs: look but do not touch
abs gives read-only access. Use it for every parameter that a verb only
needs to read. Then the caller can see from the call that its value is safe:
show reads the value twice, and score is still ours afterwards. If a verb
tries to change something it only has a view of, the compiler refuses:
The mistake is in change: it said abs, so it may not write. The fix is to
declare what you really mean. If the verb should change the value, it takes
ins.
Views freeze the owner
You can also make a view in a variable with ref:
abs view = ref n;. While a view of n exists, n is frozen. Nobody can
change it, because the view would then see a value that moved under it:
The message names what you tried (here, assigning to n) and why it is not
allowed. The view view comes from ref n, so n += 1; is not allowed while
view is in scope. If you try to move n instead, for example with
erg other = n;, the message is the shorter cannot move frozen binding.
A view cannot outlive its owner
A view only makes sense while the value it looks at exists. A verb cannot return a view of its own local variable:
ins: borrow it to change it
ins lends a value to a verb so that the verb can change it. When the call
ends, the value is back with its owner, and it keeps the change:
bump added 1 twice, and n is 3. The caller writes ins n to say: yes, I
allow this call to change n. A verb can take more than one loan:
Only one loan at a time
A loan is exclusive. While a verb may change a, nothing else may look at
a or change it. This is the rule that stops many bugs. Passing the same
variable as two loans is refused:
The same holds for a loan together with a view of the same variable. The
verb could read amount after changing target, and get a surprise:
The call must match the declaration
The role in the call has to match the role in the declaration. If a verb
asks for ins, abs is not enough, and the other way around:
Promising too much or too little are both errors. If you get
argument does not satisfy role, compare the role at the call with the role
in the declaration.
dat: hand it over
dat moves a value to the verb. From then on the verb owns it, and the
caller must not use it again:
finish takes the counter and returns its count. It is the last use of
counter in main. If main uses it again, the compiler stops it:
You will recognise this message. It is the same one you got in
Variables and values when
a String was moved with erg second = first;. A move is a move, whether it
is an assignment or a dat call.
A number is different. It is a simple value, and dat hands over a copy of
it, so the caller can still use its own. The argument can be a calculation
too:
This holds for the same types that erg second = first; copies. A struct
moves when you pass it with dat, as Counter did above.
Use dat when a verb should finish with a value for good, such as closing a
file or turning one value into another. If the verb only reads or changes it
for a moment, use abs or ins.
Reading data flow from the calls
Now look at the calls in one program. You can tell what happens to counter
without reading the verbs:
In main:
show(counter: abs counter)only reads it.bump(counter: ins counter)changes it, and it comes back.finish(counter: dat counter)takes it. It is the last line that uses it.
Each call line tells you, on its own, whether the value is only read, changed or consumed. You do not have to open the verbs to know. This is the reason for the roles. When you read a program, you can follow the life of a value line by line. When the program changes, the compiler checks again that every change still follows the rules. A change that breaks one of these promises is found at compile time, not in a running program.
What the compiler refuses
These are the errors from this page. Each is a promise that was broken:
| Message | What happened |
|---|---|
cannot mutate read-only binding | A change through abs. |
cannot assign to frozen binding, cannot move frozen binding | A change to a value, or a move of it, while a view of it exists. |
exclusive loan aliases resource ... more than once | Two loans, or a loan and a view, of the same variable. |
argument does not satisfy role of ... | The role in the call does not match the declaration. |
use of moved binding | A value used after it was moved. |
borrow ... cannot escape its scope | A view returned from the place its value lives in. |
What you learned
ergowns a value.absonly looks.insborrows to change and gives it back.dathands the value over for good.- The role is written in the declaration and again in the call, and they must match.
- While a view or a loan exists, the owner cannot be changed in other ways.
- After a
datcall or an assignment, the old name cannot be used when the value is a struct or aString. A number is copied, and the old name keeps working. - The roles make the flow of data readable from the calls alone.
Next: Errors: Result and Option.