ScottyBlades
ScottyBlades

Reputation: 13993

Argument type 'Error' does not conform to expected type Swift.Error

Why is passing an error give me Argument type 'Error' does not conform to expected type 'Swift.Error'?

enum AltError: Error {
    case error(Error), initializesWereNil
}


enum ErrorAlt<Preferred, Error> {
    
    case preferred(Preferred)
    case error(AltError)
    
    init?(_ preferred: Preferred?, _ err: Error?) {
        if let preferred = preferred {
            self = .preferred(preferred)
        } else if let passedError = err {
            self = .error(.error(passedError))
        } else {
            self = .error(.initializesWereNil)
        }
    }
}

Upvotes: 0

Views: 1639

Answers (2)

Suyash Srijan
Suyash Srijan

Reputation: 719

It is generally a good idea to not use names that conflict with names declared in other modules.

You can either replace Error generic parameter with some other name or explicitly constraint to Swift's Error type using the module name:

enum ErrorAlt<Preferred, Error: Swift.Error>

Upvotes: 1

Frankenstein
Frankenstein

Reputation: 16341

Remove the generic type Error from the ErrorAlt definition:

enum ErrorAlt<Preferred> {
    //...
}

Upvotes: 1

Related Questions