E4110#
The loop is not expected to yield a value, please remove the argument of the
break or add a nobreak branch.
This error occurs when using a break statement with an argument in a loop that
is not expected to yield a value. This applies to:
whileloopsforloops with update expressionsfor .. in ..iteration loops
These loop constructs do not have a mechanism to return a value from the loop body. If you need to break with a value, you must either:
Remove the argument from the
breakstatement if you don't need to return a value, orAdd a
nobreakbranch to handle the case when the loop completes normally and provide a return value
Erroneous example#
///|
pub fn f(x : Int) -> Unit {
for i in 0..<=x {
break i
// ^^^^^^^^ Error: The for loop is not expected to yield a value, please
// remove the argument of the `break` or add an `else` branch.
}
}
Suggestion#
To fix this error, you can:
Remove the argument from the
breakstatement. For example,
///|
pub fn stop_at_zero(x : Int) -> Unit {
for i in 0..<=x {
if i == 0 {
break
}
println(i)
}
}
Add a
nobreakbranch to handle the case when the loop completes normally and provide a return value. For example,
///|
pub fn first_index_or_default(x : Int) -> Int {
for i in 0..<=x {
break i
} nobreak {
42
}
}