[swift-evolution] [Proposal] Enums with static stored properties for each case

Charles Srstka cocoadev at charlessoft.com
Thu May 26 18:28:03 CDT 2016


> On May 26, 2016, at 4:47 PM, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org> wrote:
> 
> - Abusing rawValue is just that: an abuse.

In addition, enums with associated types can’t have rawValues.

Why is this relevant, you may ask? Because error enums are a huge use case for something like this. Being able to do the below would be great:

enum MyError: ErrorProtocol {
    accessor var localizedFailureReason: String
    accessor var url: NSURL

    case FileNotFound(url: NSURL) {
        self.localizedFailureReason = “File \"\(url.lastPathComponent ?? “”)\” not found.”
        self.url = url
    }

    case FileIsCorrupt(url: NSURL) {
        self.localizedFailureReason = “File \"\(url.lastPathComponent ?? “”)\” is corrupt.”
        self.url = url
    }
}

This would be much cleaner than the existing method of using a switch to create a userInfo dictionary for creating an NSError to send to -[NSApplication presentError:] and similar methods.

Charles

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


More information about the swift-evolution mailing list