E4133

E4133#

This for .. in loop has incorrect number of loop variables.

MoonBit supports only one or two loop variables in for .. in loop.

  • One loop variable is used for the content of the iterable.

  • Two loop variables are used for the index and the content of the iterable respectively.

Erroneous example#

pub fn bad_loop() -> Unit {
  for a, b, c in [1, 2, 3] { // Error: This `for .. in` loop has 3 loop variables, but at most 2 is expected.
    println("\{a}, \{b}, \{c}")
  }
}

Suggestion#

If you want to iterate over the index and the content of the iterable, you can use two loop variables:

pub fn iterate_with_index() -> Unit {
  for i, v in [1, 2, 3] {
    println("\{i}: \{v}")
  }
}

If you want to iterate over a iterable of tuples, then you need to explicitly destructure the tuple inside the loop body:

pub fn iterate_tuples() -> Unit {
  for v in [(1, 2, 3), (4, 5, 6)] {
    let (a, b, c) = v
    println("\{a}, \{b}, \{c}")
  }
}