Slide 6 of 74

Variables

Declaring
proc main(): void {
	x := 1                        // inferred, immutable
	mut count := 0                // inferred, reassignable
	mut Str[dyn] rows = []        // written out: an empty vector says nothing itself
	MAX := 100                    // immutable, and spelled like a constant

	count = count + 1
	append(rows, "first")
	echo "{x} {count} {rows} {MAX}"
}
One name per block
proc main(): void {
	mut total := 10
	// total := 20        // compile error: `total` is already declared in this block
	total = 20            // an assignment, which is what that line meant

	_ := total * 2        // thrown away, on purpose
	_ := total + 1        // and again: `_` is never declared
	echo total
}

x := value declares a variable and infers its type from the value, including a vector's length. Type x = value writes the type out instead, which is how a value that says nothing about its own shape — an empty vector, a new map — gets one, and the only way to say [dyn]: a length read off the value is a length all the same, and only a vector declared [dyn] can grow — append, below, is how.

A variable is immutable unless it is declared mut. An immutable one can be neither reassigned nor written through (v[0] = …, u.name = …), whatever its spelling: UPPER_CASE is a convention for such a variable, and mut is what decides.

A name is declared once per block. Declaring it again in the same block is a compile error — assign to it instead — while a block inside may declare its own, which hides the outer one until the inner block ends. _ := value throws a value away, as often as you like.