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

Radosław Pietruszewski radexpl at gmail.com
Thu Mar 31 07:15:49 CDT 2016


> On 23 Mar 2016, at 11:13, 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 ]
>    }

Ah, that’s interesting! This would make enum cases less like values, and more like their own types.

(Or maybe not — it’s still a property of the enum itself, and the definitions must cover all cases. But it does suggest a different way of thinking about what enum cases are. Maybe it would be a good thing to have enum cases be more like types, and have their own properties and stuff? I don’t know…)

— Radek

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


More information about the swift-evolution mailing list