[swift-evolution] Working with enums by name

Leonardo Pessoa me at lmpessoa.com
Wed Jun 1 17:07:21 CDT 2016

Brent, for needing "both Int and Double values" there is a proposal to add tuples instead of the current raw values or allowing assessor properties per case, you should check those out. Perhaps this could also be used to cryptoghaphically sign a raw value but I'm not sure.

As for working with enum values by name a few examples have already been posted in today but I've done a lot more research in the subject along the day and found there is a correlation between enums and nominal level values in statistics; we cannot test them for a particular order (this could also be interesting for statistic apps but it's another case) and no math with them is valid. So, e.g., the result of the following operation on the planets enum is nonsense:

|   let planet = Planet(rawValue: Planet.Mars.rawValue - Planet.Mercury.rawValue)

The result will be different if the enum values are zero based than if not. Also any change in list order or the base index or add a new element to the middle of the list will break your intended code if you're storing the raw value in a database. And we know these changes happen. Actually, given this characteristic of nominal types (statistic), we should vow to removing init(rawValue:) completely from the language.

The real value you're working with in enums is the enum case name not any associated values. By working with the name you're safe should any associated value change, should their order change, you'll only break your app if the case is removed/renamed (with the raw value, you risk having the wrong treatment being given should another enum case takes the value of a removed one).

I agree there are lots of important and more difficult things to review in the language but I wouldn't be wasting my time here if I didn't think this was equally important.


-----Original Message-----
From: "Brent Royal-Gordon" <brent at architechies.com>
Sent: ‎01/‎06/‎2016 06:10 PM
To: "Leonardo Pessoa" <me at lmpessoa.com>
Cc: "Vladimir.S" <svabox at gmail.com>; "swift-evolution" <swift-evolution at swift.org>
Subject: Re: [swift-evolution] Working with enums by name

> This should work but feels like an ugly hack to me. What if I needed
> the enum like this?
> |   enum Size : Double {
> |       case Fit = 0.5
> |       case Fill = 3.0
> |   }

What if you needed both Int and Double rawValues? What if you needed rawValues that were cryptographically signed? We have to decide which use cases are common enough to support directly in the language, and I'm not convinced that "I need to look cases up by name, but I have no choice but to use rawValue for something else" is one of them—that is, that it's *so* common that we need to direct our scarce engineering resources towards designing and implementing a separate feature merely to accommodate it. There are a whole lot of things that are *way* higher on our to-do list than this, arguably including metaprogramming features which would let you write this yourself instead of sticking it in the core language.

Brent Royal-Gordon

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

More information about the swift-evolution mailing list