4 ms·
Oof, magic strings. Always use enums! enum Booze: String { case wine, case champagne, case beer, case whiskey } le
by dep_b 9y ago
Oof, magic strings. Always use enums!
enum Booze: String {
case wine,
case champagne,
case beer,
case whiskey
}
let wine = [Booze.champagne]
</ocd>
- golergka 9y agoI feel like adding a pull request to add whisky to the list. Some of us prefer scotch.
- ryan-koch 9y agoDefinitely have to +1 this. I'd be happy to write some tests to get scotch on board.
- koolba 9y agoI'm not sure if it was intentional but "case beer" made me smile.
- meggar 9y agowine's type should conform to a Stack protocol so you can pop champagne.
- rimliu 9y agoWhy are these String, not just enums?
- dep_b 9y agoBecause it's replacing a Magic String?
- timjver 9y agoYou don't need commas :)
- abalone 9y agoNooooooo... you're right about enums but this codifies the same error I was trying to elucidate with code. Here, let's try this: enum Booze { case Wine(region: String) case Beer case Whiskey } let drink = Booze.Wine(region: "Champagne") Now.. Anyone want to demonstrate how to use enums for the wine region or, say, whiskey subtypes? Should we use classes?