Skip to content
ZOLO / TE159 Type · error

`if let x = …` binds a whole `Option`/`Result`

Compiler diagnostic TE159: `if let x = …` binds a whole `Option`/`Result`.

if let x = value with a bare name narrows a nullable T?: the branch runs when the value is not nil, with x of type T. An Option or a Result is not nullable — it is a wrapper that is never nil — so a bare name matches it every time and binds the wrapper itself. The branch always runs, and x is the Option, not the value inside it. With while let, the loop never stops.

let xs = [10, 20, 30]
if let first = xs.first() {
//     ^^^^^ error[TE159]
    print(first + 1)
}

Fix it

Name the variant whose value you want:

let xs = [10, 20, 30]
if let .Some(first) = xs.first() {
    print(first + 1)       // 11
}

For a Result, the variant is .Ok(value). zolo fix and the editor's quick fix rewrite the bare name to .Some(name) (.Ok(name)).

Global index

Find your way through Zolo

Try an idea

Start here

9 results

9 results

en