machdocs
Home Ecosystem Changelog

Values and variables

Bindings introduce named values. val is immutable, var is mutable, and both require an explicit type, since a binding never takes its type from its initializer.

Declaring bindings

A binding names a value with a declared type. val is constant once set; var can be reassigned. Every form pins the type in the declaration.

val NAME: TYPE = EXPR;              # immutable; initializer required
var NAME: TYPE;                     # mutable; zero-initialized
var NAME: TYPE = EXPR;              # mutable; explicit initializer

Mutability and defaults

A val always carries an initializer and cannot be reassigned. A var may omit its initializer, in which case it is zero-initialized, and it can be written to afterward. Zero means every byte: a number is 0, a pointer is nil, every element of an array and every field of a record is zero, and a tag holds its first declared case with that case's payload zeroed.

val pi: f64 = 3.14159;
val n:  i64 = 42;

var counter: i64 = 0;
var buf:     [256]u8;               # default-initialized to zero

fun bump() {
    counter = counter + 1;          # var is reassignable
}

Assigning to a val, or to a field or element of one, is a compile-time error, for a module-level val and a local one alike:

val n: i64 = 42;

fun change() {
    n = 43;                         # error: cannot assign to `n`: a `val` is immutable
}

A val of pointer type binds the address immutably, not the storage it addresses, so @p = x and p.field = x write the pointee and are legal.

Scope and exports

Both forms work at module top level and inside function bodies. Inside a function, a binding is local to its enclosing block. At module top level, pub exports it:

Module-level initializers

Every module-level initializer must have a compile-time value. One that has none, such as a call or a read of another var, is refused:

val FIRST: i64 = next();          # error: global initialiser of `FIRST` must be a constant expression

A module-level val of integer, bool or float type whose initializer is a constant is that value at every use site, in its own module and in every module that imports it, and its storage is still emitted so ?NAME has an address. A val of record, array or vector type is laid out as constant data, built from literals, other constant vals, strings, nil, functions named bare, and addresses ?place of module-level storage.

A module-level integer may also hold a number made of addresses, which the link fixes: an address cast to a pointer-wide integer, with a constant added or taken away, or the distance between two addresses.

var start: [64]u8;
var end:   u8;

val BASE: u64 = (?start)::u64;                          # the address as an integer
val PAST: u64 = (?start[8])::u64 + 16;                  # an address with a constant added
val SPAN: u32 = ((?end)::u64 - (?start)::u64)::u32;   # the distance, in 4 bytes

The width the link fills must be 4 or 8 bytes. Pointer arithmetic itself stays refused: ?start + 1 is not one of these forms.

No type inference

Every binding declares its type. An untyped numeric literal is checked against the binding's declared type; it never participates in inferring that type.

val n: i64 = 42;                    # ok - 42 conforms to i64
val x      = 42;                    # ERROR - a binding needs an explicit type
val y      = 42i64;                 # ERROR - a suffix is not an annotation
Note

A typed suffix such as 42i64 gives the literal a type, never the binding. Suffixes earn their keep where there is no annotation to read from, such as the arguments of a variadic pack.

The one part of an annotation an initializer may supply is an array length: [_]T takes its length from the array literal or string literal that initializes the binding, as in val tag: [_]u8 = "abc";, a [3]u8. See Types.

See also

  • Types - the type grammar used in the annotation
  • Visibility - how pub exports bindings
  • Functions - parameters and return values