[swift-evolution] [Idea] Find alternatives to `switch self`

Charles Srstka cocoadev at charlessoft.com
Fri Apr 8 16:30:57 CDT 2016


> On Mar 23, 2016, at 5:13 AM, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org> wrote:
> 
> * Allow you to attach member definitions to particular cases. It would be an error if they didn't all define the same members, unless there was a top-level catchall.
> 
>    enum Suit: Int {
>        var isRed: Bool { return false }
> 
>        case Hearts {
>            let description: String { return "♥️" }
>            let isRed: Bool { return true }
>        }
>        case Spades {
>            let description: String { return  "♠️" }
>        }
>        case Diamonds {
>            let description: String { return  "♦️" }
>            let isRed: Bool { return true }
>        }
>        case Clubs {
>            let description: String { return  "♣️" }
>        }
> 
>        static var all = [ Hearts, Spades, Diamonds, Clubs ]
>    }

Oh my, I absolutely *love this.* This would be a godsend for making ErrorType enums that bridge nicely to NSErrors, with code and userInfo properties attached to each case.

+1.

Charles

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


More information about the swift-evolution mailing list