E4134#
Compiler diagnostic name: invalid_foreach_pattern.
A pattern in a for .. in binding is not exhaustive for the value being
iterated over.
The loop body runs once for every item, so its binding pattern must match every
possible item. For example, Some(value) cannot bind an Option[T], because an
item may be None.
Erroneous example#
///|
pub fn print_values(values : Array[Option[Int]]) -> Unit {
for Some(value) in values {
println(value)
}
}
Suggestion#
Bind the whole item and handle alternatives inside the loop, or use an exhaustive destructuring pattern such as a tuple or record pattern.
///|
pub fn print_values(values : Array[Option[Int]]) -> Unit {
for value in values {
match value {
Some(value) => println(value)
None => ()
}
}
}