ErrorType protocol's hidden requirements

Both the documentation on ErrorType and the description given when you command-click on ErrorType in Xcode give ErrorType as:


protocol ErrorType {
}


Which is, apparently, a lie. Attempting to add ErrorType to a struct's conformances gives "does not conform to protocol 'ErrorType'" errors.


Inspecting any ErrorType using LLVM reveals that the ErrorType definition is really:


protocol ErrorType {
     var _domain: String { get }
     var _code: Int { get }
}


Sure enough, if you implement these hidden methods, any type can conform to ErrorType.

Bringing me to my questions...


  1. Why is this requirement not exposed for us to use normally? Are we required to limit ErrorType to Swift enums and NSError for now?
  2. Related question: since ErrorType can extract an Int "tag" from an arbitrary enum, is there any public way to do the same without going enum -> ErrorType -> NSError -> code?

For question 1, I think this is a work-around for now to enable bridging of NSError into Swift from ObjC and vice-versa. I've simply been using enums with associated values for all of my error types.


For question 2, I don't know if this is correct or stable, but one possible way for basic enums is:


enum MyEnum {
    case Hello
    case World
}
MyEnum.Hello.hashValue   // 0
MyEnum.World.hashValue   // 1

The hashValue method is only valid for enums which have no cases with associated values. My interest in the ErrorType._code is that it is valid for *all* enums.

Yeah, I agree... I just know of a way to do that. :/

ErrorType protocol's hidden requirements
 
 
Q