A variable declaration consists of a name and a type.

a, b, c: int;

Variable declarations can also have initializers.

a, b, c: int = 1, 2, 3;

The type can be omitted; the variable will take the type of the initializer.

a, b, c := true, "Hellope!", 1.2;

Zero values

Variables declared without an explicit initial value are given their zero value.

The zero value is:

  • 0 for numerical types,
  • false for boolean types,
  • "" (empty string) for strings
  • nil for pointers, slices, maps, dynamic arrays, procedures, and any

Type inference

When declaring a variable without an explicit type, the variable's type is inferred from the value on the right hand side.

When the right hand side of the declaration is typed, the declared variable is of that same type.

i: int;
j := i; // j is an `int`

But when the right hand side contains an untyped constant, the new variable will take on its default type.

i := 42;               // int
f := 6.28;             // f64
g := 2.3 + 1i;         // complex128
h := 1 + 2i + 3j - 4j; // quaternion256
s := "hellope";        // string
b := true;             // bool


If a variable is in the global/file scope, it can be marked to be allocated in thread local storage with #thread_local.

#thread_local some_name: int;

A thread local variable cannot be declared within a procedure nor can it have an initial value.

A variable declaration can be denoted as immutable. This means that it and its contents cannot be changed once declared and initialized.

immutable x := 123;
x = 123; // Error

immutable is "viral" in that any indirection is also immutable too.

i := 123;
immutable p := ^i;
p^ = 123; // Error even though `p` is not being changed

results matching ""

    No results matching ""