E4211#
Compiler diagnostic name: control_in_list_comprehension.
Control flow is not allowed inside a list comprehension.
A list comprehension builds a new collection from the elements produced by its
clauses. Control operations such as break, continue, return, raise, and
async control do not have a surrounding loop or function boundary inside the
comprehension body that can consume them.
Erroneous example#
pub fn values() -> Array[Int] {
label~: for ;; {
let xs = [for _ in 0..<3 => { break label~ }]
return xs
}
[]
}
The break appears inside the list comprehension body, so MoonBit reports an
error.
Suggestion#
Move the control flow outside the comprehension, or rewrite the code as an explicit loop.
pub fn values() -> Array[Int] {
[for value in 0..<3 => value]
}