[swift-evolution] [Accepted] SE-0112: Improved NSError Bridging

Charles Srstka cocoadev at charlessoft.com
Sun Aug 14 02:03:13 CDT 2016


> On Aug 14, 2016, at 1:18 AM, Jon Shier via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 	An immediate problem I’m seeing is the error: using 'Error' as a concrete type conforming to protocol 'Error' is not supported, which means we can’t use Error in our Result or Response types, as both the value and error types are generic there. I’m guessing we’ll have to either remove the generic error type, which would greatly impact consumer’s ability to use our APIs with their own custom errors, or wrap all errors in our own error type, which is terrible for a few reasons. Or just keep using NSError I guess. Any clever solutions here?

Works fine when I try it:

import Foundation

struct MyThing {
    enum Error: Swift.Error, LocalizedError {
        case doesNotCompute
        case imSorryDave
        case mustSterilize
        
        var failureReason: String? {
            switch self {
            case .doesNotCompute:
                return "Does Not Compute! Does Not Compute! Does Not Compute!"
            case .imSorryDave:
                return "I'm sorry Dave, I'm afraid I can't do that."
            case .mustSterilize:
                return "Error! Must Sterilize! Must Steeerrrrilllliiiiiiizzzzzzeeeeeee"
            }
        }
    }
    
    func trySomething() throws {
        throw Error.doesNotCompute
    }
}

let thing = MyThing()

do {
    try thing.trySomething()
} catch {
    print(error.localizedDescription)
}

Outputs:

The operation couldn’t be completed. Does Not Compute! Does Not Compute! Does Not Compute!

Charles

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160814/06c9f488/attachment.html>


More information about the swift-evolution mailing list