Broken code snippet in the Overview's "Range-based for loop" section

Range-based for loops can also iterate over the results of a procedure that returns a multi-valued tuple whose last value is a bool. The loop continues while the boolean is true, and the preceding values in the tuple are available as loop variables. This is similar in concept to a “custom iterator” but is just syntactic sugar for calling the procedure in a loop.

iter_count := 0
some_procedure :: proc() -> (int, int, int, cond: bool) {
	iter_count += 1
	if iter_count < 4 {
		return iter_count, iter_count*2, iter_count*3, true
	} else {
		return 0, 0, 0, false
	}
}

for a, b, c in some_procedure() {
	fmt.println(a, b, c)
	// Prints
	// 1 2 3
	// 2 4 6
	// 3 6 9
}

If this is a typo, the fix is to remove the cond identifier from the last return value: (int, int, int, bool).
If this is a type error, then there’s a problem.
Given the code context, the procedure returns 4 values: 3 unnamed ints and 1 named bool.
But to Odin, it looks as if the procedure returns 4 named bools:
(int, int, int, cond: bool) => (int: bool, int: bool, int: bool, cond: bool) .
It gives the following errors:

Direct shadowing of the named return value 'int' in this scope 
Cannot assign value 'iter_count' of type 'int' to 'bool' in return statement 
Cannot convert '0' to 'bool' from 'untyped integer', got 0 - Did you want 'false'? 

Thus, there are two options:

  • List the types only: (type_1, type_2, ..., type_n)
  • Name each return value (name_1: type_1, name_2: type_2, ..., name_n: type_n)
1 Like