[swift-evolution] Working with enums by name

Vladimir.S svabox at gmail.com
Wed Jun 1 16:44:32 CDT 2016


Can't agree with Brent's opinion. For me the proposed init(caseName:String) 
and .caseName property for enum case - seems like  base feature that enum 
type must to have.

As for string as raw value for enum, please find this example:

enum E: String {
     case one = "One"
     case two = "Two"
}

print(E.one.rawValue)  // One
print(E(rawValue: "one")) // nil
print(E(rawValue: "One")) // Optional(main.E.one)

How do you suggest to transform the case name("one") to enum case variable? 
`init(rawValue:)` will accept assigned rawValue, not case name. Even worse, 
conforming to CustomStringConvertable can prevent you from have "one" as 
result String(E.one).

On 02.06.2016 0:10, Brent Royal-Gordon wrote:
>> 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.
>


More information about the swift-evolution mailing list