0

Consider the code below:

// OK
let res1: Result<Int, Error>? = nil

protocol MyError: Error { }

// Protocol 'MyError' as a type cannot conform to 'Error'
let res2: Result<Int, MyError>? = nil

Why cannot I use MyError protocol which extends Error here, is this behaviour is explained somewhere in the official docs?

And another example:

// Protocol 'MyProtocol' as a type cannot conform to the protocol itself
protocol MyProtocol { }
class MyClass<T: MyProtocol> { }
let c = MyClass<MyProtocol>()

// OK
class MyOtherClass<T: Error> { }
let c2 = MyOtherClass<Error>()

I might guess that Error is being bridged to NSError and used as a concrete class, not a Swift protocol; but when we're using MyError that logic does not kick in. Any references?

Dannie P
  • 4,306
  • 3
  • 28
  • 47
  • `Error` definitely has some magic behind it but the error is quite explicit. You'll need to use a concrete type that conforms to `MyProtocol` to initialize `MyClass`. – EmilioPelaez Jul 06 '21 at 13:54

0 Answers0