[swift-evolution] Working with enums by name

Patrick Smith pgwsmith at gmail.com
Thu Jun 2 00:07:13 CDT 2016


Great points Brent. I think the ValuesEnumerable method would be the most straight forward. Also, the number of cases are likely only going to be in range of 6–20, so iterating would be fine I think. People can create something like `Dictionary(Planet.allValues.enumerated().lazy.map{ ($1, $0) })` (I think that’s right) if they really need.


> On 2 Jun 2016, at 2:40 PM, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Or the `ValuesEnumerable` proposal would give you a convenient, though slightly slow, way to do two-way lookup by order:
> 
> 	enum Planet: String, ValuesEnumerable {
> 		var order: Int {
> 			return Planet.allValues.index(of: self)!
> 		}
> 		init(order: Int) {
> 			self = Planet.allValues[order]
> 		}
> 		case mercury, venus, …
> 	}



More information about the swift-evolution mailing list