E4104

E4104#

当前循环的参数数量与 continue 提供的参数数量不同。

此错误发生在 continue 语句提供的参数数量与循环所期望的参数数量不匹配时。在 loop 构造中,使用 continue 时,必须提供与循环头中声明的相同数量的参数。

例如,如果一个 loop 有 2 个参数,那么任何在该 loop 中使用 continue 的语句都必须提供 2 个参数。参数数量不足或过多都会触发此错误。

请注意,在 for 循环中,你可以省略 continue 语句中的所有参数。在这种情况下,循环将使用循环头中指定的默认更新表达式。但是,如果你提供了参数给 continue,那么参数数量必须与循环变量数量匹配。

例如,在一个有两个变量的 for 循环中:

  • continue(没有参数)将使用默认更新

  • continue x, y(有两个参数)是有效的

  • continue xcontinue x, y, z 会触发此错误

错误示例#

pub fn f(x: Int, y: Int) -> Int {
  loop x, y {
    0, 0 => 0
    a, _ => continue a - 1
//          ^^^^^^^^^^^^^^ Error: Current loop expects 2 arguments, but
//                                `continue` is supplied with 1 arguments
  }
}

pub fn g(x : Int, y : Int) -> Int {
  for i = x, j = y; i + j < 10; i = i + 1, j = j + 1 {
    if i < j {
      continue i + 2, j + 1, i + j
//    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Error: Current loop expects 2 arguments, but
//                                       `continue` is supplied with 3 arguments
    }
  } else {
    42
  }
}

建议#

要修复此错误,请确保 continue 提供的参数数量与循环变量数量匹配。例如:

pub fn f(x: Int, y: Int) -> Int {
  loop x, y {
    0, 0 => 0
    a, b => continue a - 1, b - 1
  }
}

pub fn g(x : Int, y : Int) -> Int {
  for i = x, j = y; i + j < 10; i = i + 1, j = j + 1 {
    if i < j {
      continue i + 2, j + 1
    } else {
      continue
    }
  } else {
    42
  }
}