Compiler makes incorrect assumption. Thoughts?

In the following code, I received the error 'variable used within its own initial value'. Surely the complier should appreciate that I am referring to the parameter in the function signature, and not the parameter that failed to initialise?

private func handle(error: Error) {
guard let error = error as? FetchError else {
    let controller = makeAlert(for: error) // variable used within its own initial value
    present(controller, animated: true)
    return
    }
    //etc
}

Yeah, it’s a known issue. The error in the guard let is shadowing the error argument. You can workaround it by renaming the variable to something else.

(IMO it would be clearer as well when these two variables have different names. I can imagine someone being confused when reading the code which value is actually being used)

You can track it in SR-8669.

1 Like
Terms of Service

Privacy Policy

Cookie Policy