[swift-evolution] Working with enums by name

Patrick Smith pgwsmith at gmail.com
Thu Jun 2 21:18:21 CDT 2016


From what I understand, enums normally are represented internally by an offset — that is their truth. With RawRepresentable enums, you are saying “no, I want the truth to be something else”. But it seems that they are still represented internally by an offset, so you can’t reorder a RawRepresentable enum’s cases and maintain ABI compatibility either.

So what you are saying about the order of cases being an intrinsic part of an enum does make sense. I’m not sure if can still lead to confusing / fragile code though.

Patrick


> On 2 Jun 2016, at 10:17 PM, Leonardo Pessoa <me at lmpessoa.com> wrote:
> 
> There are several ways to solve this, which IMO is a basic functionality of enums, writing code that is currently possible and works. But that's the issue, you still have to write code to have a basic functionally. I don't remember not being able to do this out-of-the-box in any language I worked with.
> 
> L
> From: Patrick Smith <mailto:pgwsmith at gmail.com>
> Sent: ‎02/‎06/‎2016 02:07 AM
> To: Brent Royal-Gordon <mailto:brent at architechies.com>
> Cc: Leonardo Pessoa <mailto:me at lmpessoa.com>; swift-evolution <mailto:swift-evolution at swift.org>
> Subject: Re: [swift-evolution] Working with enums by name
> 
> 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, …
> > }
> 

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


More information about the swift-evolution mailing list