Types, patterns and valuesSlide 28 of 86
28[3]Types, patterns and values

A length is a promise

Grammar: typed-decl, dim
muttypeIDENT=expressiontypeIDENT=asynccall
typed-decl = [ "mut" ] type IDENT "=" expression | type IDENT "=" "async" call ;As the compiler reads it; §2.5 of the specification is out of date here.
[][dyn][INT]
dim = "[" "]" | "[" "dyn" "]" | "[" INT "]" ;

A declared static length is kept everywhere a value reaches it. Every value that lands in a Str[3] — an initialiser, a later assignment, an argument, a field of a constructed value, a returned value — has to be exactly three long, and a length the compiler cannot see is refused, which is why split can never fill one. Because the promise holds at every one of those places, a Str[3] can be indexed unguarded even after it is reassigned, and so can a Str[3] parameter inside the callee. A Str[3] field is the same, construction included: assigning a whole new record still leaves it exactly three.

An inferred length is a length too — mut v := ["a", "b", "c"] can only be rebound to another three, and never grows — but the bounds pass keeps less about it. Writing through the name keeps the proof. Rebinding the name loses it, even to the same length and even in a branch that may not run, since a proof that holds only sometimes is not a proof. Declare the type when you want the promise, and [dyn] when you want a vector that grows and whose indexes you will guard.

A guard proves less again: if 2 < len(box.items) is about the value the field held at that moment, so replacing the field, or the record around it, loses the proof, even inside the branch the guard opened.

A known length survives every operation that cannot change it: + adds the two lengths, and map and sort keep their input's. filter and filterMap claim no length. All that is known about theirs is a maximum, and a maximum never puts an index in range, since a filter may keep nothing.

A declared length is kept
proc main(): void {
	mut Str[3] v = ["a", "b", "c"]
	v = ["x", "y", "z"]   // still exactly three: the promise is kept
	echo v[2]              // no guard needed, here or ever

	// v = ["x"]           // compile error: `v` is declared Str[3]
}
An inferred one keeps its length, and less of its proof
proc main(): void {
	mut v := ["a", "b", "c"]   // a Str[3]: rebound only to three, never grown
	v[0] = "x"                  // a write through the name keeps the length
	echo v[2]

	// v = ["x"]               // compile error: `v` is a Str[3]
	// v = ["x", "y", "z"]      // fine, but a rebinding costs the proof,
	// echo v[2]                // so this would need a guard
}
What a known length survives
func twice(n: Int): Int { return n * 2 }

proc main(): void {
	Int[2] a = [2, 1]
	Int[8] b = [1, 2, 3, 4, 5, 6, 7, 8]

	both := a + b            // ten elements: the lengths add
	echo both[9]              // no guard needed

	doubled := map(a, twice)  // still two
	ordered := sort(a)        // still two
	echo "{doubled[1]} {ordered[1]}"

	// filter and filterMap claim nothing, so their results always need one.
}
A field's length is a promise too
type Reading {
	sectors: Float[3]     // three, in every Reading there will ever be
	notes:   Str[dyn]     // any number, in each one separately
}

func slower(was: Reading): Reading {
	return Reading([was.sectors[0] + 0.1, was.sectors[1], was.sectors[2]], was.notes)
}

proc main(): void {
	mut Reading lap = Reading([31.4, 28.9, 33.2], ["clean"])
	echo lap.sectors[2]        // no guard: the declaration promised three

	lap = slower(lap)          // a whole new record...
	echo lap.sectors[2]        // ...and still three, so still no guard

	// The other way round for a [dyn] field, where only a guard can say:
	if lap.notes bounds 0 {
		echo lap.notes[0]
	}
}