Wednesday, August 19, 2015

Testing Swift’s ErrorType

Marius Rackwitz:

Further investigation via LLDB reveals that the protocol has some hidden requirements.

(lldb) type lookup ErrorType
protocol ErrorType {
  var _domain: Swift.String { get }
  var _code: Swift.Int { get }
}

It becomes clear how NSError fulfills this definition: it has these properties, backed by ivars, which Swift can access without dynamic dispatch. What is still unclear is how Swift’s first class enums can automatically fulfill this protocol. Perhaps there is still some magic involved?

[…]

Sure, you could catch it as any generic error and then manually check the fields _domain and _code, but there are more elegant alternatives.

Comments RSS · Twitter

Leave a Comment