Variables and values

View source

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:

actus
verb main() {
    erg count: Int = 5;
    erg doubled = count * 2;
    erg ready = true;
    erg greeting = "hello";
    println(int: count);
    println(int: doubled);
    println(bool: ready);
    println(string: greeting);
}
bash
actus run
text
5
10
true
hello
process exited with status 0

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:

actus
# This line is a comment.
verb main() {
    erg count: Int = 5; # So is the end of this line.
    println(int: count);
}
bash
actus run
text
5
process exited with status 0

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:

EscapeCharacter
\na new line
\ta tab
\ra carriage return
\\a backslash
\"a double quote
\u{...}a Unicode character, named by 1 to 6 hex digits
actus
verb main() {
    println(string: "one\ntwo");
    println(string: "name:\tAda");
    println(string: "she said \"hi\"");
    println(string: "a backslash: \\");
    println(string: "snowman: \u{2603}");
}
bash
actus run
text
one
two
name:	Ada
she said "hi"
a backslash: \
snowman: ☃
process exited with status 0

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:

actus
verb main() {
    println(string: "a\0b");
}
bash
actus check
text
/home/you/hello/src/main.act: error[E0028] at 2:23: invalid string escape `\0`: a string literal cannot hold the NUL character, because a `String` ends at its first NUL byte

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:

actus
verb main() {
    erg score = 10;
    score = score + 5;
    score += 1;
    println(int: score);
}
bash
actus run
text
16
process exited with status 0

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:

actus
verb main() {
    erg score = 10;
    score = "ten";
    println(int: score);
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1027] at 3:5: type mismatch for `score`: expected `Int`, found `String`

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:

actus
verb main() {
    erg total: Int;
    println(int: total);
}
bash
actus check
text
/home/you/hello/src/main.act: error[E0003] at 2:19: expected `=`, found `;`

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:

actus
verb main() {
    erg count = 1;
    erg count = 2;
    println(int: count);
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1001] at 3:5: duplicate binding `count`

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:

actus
verb main() {
    erg count = 1;
    if count == 1 {
        erg count = 2;
        println(int: count);
    }
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1002] at 4:9: shadowed binding `count`

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:

actus
verb main() {
    erg ready = true;
    if ready {
        erg count = 1;
        println(int: count);
    }
    if ready {
        erg count = 2;
        println(int: count);
    }
}
bash
actus run
text
1
2
process exited with status 0

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:

actus
verb main() {
    erg first = 1;
    erg second = first;
    first = 2;
    println(int: first);
    println(int: second);
}
bash
actus run
text
2
1
process exited with status 0

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:

actus
verb main() {
    erg first = "hello";
    erg second = first;
    println(string: first);
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1005] at 4:21: use of moved binding `first`

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:

actus
verb main() {
    erg first = 1;
    erg second = copy(value: abs first);
    first = 2;
    println(int: first);
    println(int: second);
}
bash
actus run
text
2
1
process exited with status 0

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:

actus
const LIMIT: Int = 100;

verb main() {
    erg next = LIMIT + 1;
    println(int: LIMIT);
    println(int: next);
}
bash
actus run
text
100
101
process exited with status 0

You can read a constant anywhere in the file. You cannot assign to it:

actus
const LIMIT: Int = 100;

verb main() {
    LIMIT = 5;
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1142] at 4:5: cannot assign to constant `LIMIT`: constants are fixed at compile time; declare an `erg` binding for a value that changes

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:

actus
verb main() {
    erg level = 3;
    abs view = ref level;
    println(int: view);
    view = 4;
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1051] at 5:5: cannot mutate read-only binding `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 erg variable.
  • 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, a Bool or a float, and a stays usable. It moves any other value, such as a String, and then a cannot be used.
  • const gives a fixed name to a value, and you cannot assign to it.

Next: Numbers.