E4102

E4102#

不在循环中。

该错误发生在使用 breakcontinue 语句时,它们只能在循环中使用。

  • break 用于提前退出循环。

  • continue 用于跳过循环的下一次迭代。

在循环体外使用这些语句是无效的,因为没有循环可供跳出或继续到下一次迭代。

错误示例#

///|
pub fn f(xs : Array[Int]) -> Int {
  for i in xs {
    ignore(i)
  } nobreak {
    break 42
  }
  //  ^^^^^^^^ Error: 'break' outside of a loop
}

///|
pub fn g(x : Int) -> Unit {
  continue x
  // ^^^^^^^^ Error: 'continue' outside of a loop
}

建议#

要修复此错误,请确保 breakcontinue 在循环构造中使用。

///|
pub fn first_or_default(xs : Array[Int]) -> Int {
  for x in xs {
    if x > 0 {
      break x
    }
  } nobreak {
    0
  }
}

///|
pub fn count_down(from : Int) -> Unit {
  for i = from; i > 0; i = i - 1 {
    if i % 2 == 0 {
      continue
    }
    println(i)
  }
}