E4110

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:

  • while loops

  • for loops with update expressions

  • for .. 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:

  1. Remove the argument from the break statement if you don't need to return a value, or

  2. Add a nobreak branch 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 break statement. For example,

///|
pub fn stop_at_zero(x : Int) -> Unit {
  for i in 0..<=x {
    if i == 0 {
      break
    }
    println(i)
  }
}
  • Add a nobreak branch 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
  }
}