Skip to content
Playground(another site)

bynk.types.question_error_mismatch

Error ? propagates an error type incompatible with the function's.

? returns the Err from the enclosing function as it is, so its error type must be one the function declares: a String error cannot escape a function whose errors are Int. The only automatic conversion is a declared embedding, where the function's error is a sum type with an embeds clause for the inner one. Otherwise convert the error before the ? with .mapErr(…), or change the function's error type.

Example

fn checked(n: Int) -> Result[Int, String] {
if n >= 0 { Ok(n) } else { Err("negative") }
}
fn run(n: Int) -> Result[Int, Int] {
let v = checked(n)? // ✗ propagates a `String` error, but `run` returns `Int` errors
Ok(v + 1)
}
fn run(n: Int) -> Result[Int, Int] {
let v = checked(n).mapErr((e) => e.length())? // ✓ the error is converted first
Ok(v + 1)
}

In the Book

See also

  • The grammar rule it governs: question_expr.
  • Every code in its category, Type checking, on the diagnostic index.
  • bynk explain bynk.types.question_error_mismatch shows the same from the installed toolchain, without a network.