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

Brent Royal-Gordon brent at architechies.com
Wed Mar 23 20:22:31 CDT 2016


> enum Suit: Int {
>        case Hearts, Spades, Diamonds, Clubs
> 
>        static var all: Suit[] { return [ Hearts, Spades, Diamonds, Clubs ] }
> 
>        var description: String {
>            return match(self) {
>                case .Hearts: "♥️"
>                case .Spades: "♠️"
>                case .Diamonds: "♦️"
>                case .Clubs: "♣️"
>            }
>        }
> 
>        var isRed: Bool {
>            return match(self) {
>                case .Hearts, .Diamonds: true
>                case .Spades, .Clubs: false
>            }
>        }
>    }

This attacks the `return` boilerplate rather than the `switch` boilerplate. That means that, while it helps in this case, it would not help with members which have complicated logic or side effects. (And technically, the boilerplate is still there—it's just a `match(self)` instead of a `switch self`. I feel like they're orthogonal issues.

-- 
Brent Royal-Gordon
Architechies



More information about the swift-evolution mailing list