Variables and values
Create variables with erg, give them types, change them, and see what the compiler refuses.
A variable is a name for a value. In this page you create variables, change
them, and learn the few rules the compiler checks. Every example here is a
whole program. To try one, put it in src/main.act of the project from
First program and run actus run.
Create a variable
You create a variable with erg. Then come the name, an optional type, an
equals sign and the value:
The first line makes count with the type Int. The next lines leave out the
type. The compiler works it out from the value: count * 2 is an Int,
true is a Bool and "hello" is a String.
Write the type when you want to be explicit, or when the value alone does not say which type you mean. Numbers shows when that matters.
A # starts a comment. The compiler ignores everything from the # to the
end of the line:
The word erg is one of four ownership roles. For a local variable it means:
this scope owns the value and may change it. The other roles are explained in
Ownership roles.
Text and escapes
A String literal is text between double quotes. A backslash inside it starts
an escape, which lets you write a character that cannot stand for itself:
| Escape | Character |
|---|---|
\n | a new line |
\t | a tab |
\r | a carriage return |
\\ | a backslash |
\" | a double quote |
\u{...} | a Unicode character, named by 1 to 6 hex digits |
Any other character after a backslash is an error, and so is \0. A string
cannot hold the NUL character, because it ends at its first NUL byte. The
compiler reports E0028 for both and names the valid escapes:
Change a variable
A variable made with erg can get a new value. Use = to replace the value
and += (or -=, *=) to change it in place:
The new value must have the type of the variable. The type does not change
after the variable is created. If you try to put text into an Int, the
compiler stops before the program runs:
The message gives the error code, the line and column, and the two types.
Every variable needs a value
You cannot make a variable and fill it in later. The value is part of the line that creates the variable:
This message comes from the parser. It expected = after the type, so it
tells you that a value is missing.
One name, one variable
A name can be declared only once in the same block. A second erg with the
same name is an error:
A nested block cannot reuse the name of a variable that is still visible
from the block around it. The code is a different one, E1002:
Other languages call this shadowing. Actus does not have it. Choose a new
name, or change the existing variable with =.
Two blocks next to each other do not see each other's variables, so each of them can use the same name:
The counter of a for loop is the exception. It belongs to the loop, so two
loops one after the other can both use i. You will see this in
Conditions and loops.
Copying and moving
What erg second = first; does depends on the type of first. A number, a
Bool or a decimal number is copied. Both names keep a value, and changing
one does not change the other:
This holds for Int, Bool, Usize, the integer types such as u8 and
i64, and f32 and f64. A value of any other type moves. After
erg second = first; the value belongs to second, and the name first
can no longer be used. A String is one of these types:
A struct, an enum and an array move in the same way. You will meet structs in Structs, enums and case.
For a number you can also write the copy out with copy:
copy(value: abs first) makes a new Int and leaves first as it was.
For a number it does the same as erg second = first;, so you do not need
it. It does not work for strings or other values that own memory.
The abs inside the call is a role. It says that copy only looks at
first. You will meet it again in Verbs and
Ownership roles.
Writing an expression such as first * 2 also gives you a new value. It
reads first and does not move it. This is why erg doubled = count * 2; in
the first example left count usable.
Constants
A const is a name for a value that is fixed before the program runs. Write
it outside any verb. Its type is required:
You can read a constant anywhere in the file. You cannot assign to it:
The message says what is wrong and what to do. A constant is fixed before the
program runs, so it cannot change. If you need a value that changes, declare
an erg variable instead.
Views are read-only
abs view = ref level; makes a read-only view of level. A view lets you
look at a value without owning it. You cannot assign to a view:
Views have more rules, for example the owner is frozen while a view of it exists. Ownership roles covers them.
What you learned
erg name: Type = value;creates a variable. The type is optional when the compiler can tell it from the value.- You can assign a new value of the same type to an
ergvariable. - A variable needs a value when it is created.
- A name can be declared once in a block, and a nested block cannot reuse a name that is still visible. There is no shadowing.
erg b = a;copies a number, aBoolor a float, andastays usable. It moves any other value, such as aString, and thenacannot be used.constgives a fixed name to a value, and you cannot assign to it.
Next: Numbers.