[swift-evolution] Working with enums by name

Leonardo Pessoa me at lmpessoa.com
Wed Jun 1 06:13:44 CDT 2016


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
|   }

Then I can no longer rely on the compiler to fill string raw values
for me to use.

L

On 1 June 2016 at 07:59, Vladimir.S <svabox at gmail.com> wrote:
> Try this:
>
> enum Size: String { case Fit, Fill }
> print(Size.Fit.rawValue)
>
> On 01.06.2016 13:42, Leonardo Pessoa via swift-evolution wrote:
>>
>> Just a fix. I've just tried the following code and the compiler complained
>> there is no .rawValue on the type.
>>
>> |   enum Size { case Fit, Fill }
>> |   print(Size.Fit.rawValue)
>>
>> Then, as I said before, you can only get the value name as a string from
>> interpolation and need to do everything by hand the other way around.
>>
>> L
>>
>> ---------------------------------------------------------------------------
>> From: Charlie Monroe via swift-evolution
>> <mailto:swift-evolution at swift.org>
>> Sent: ‎01/‎06/‎2016 07:19 AM
>> To: Brent Royal-Gordon <mailto:brent at architechies.com>
>> Cc: Swift-evolution <mailto:swift-evolution at swift.org>
>>
>> Subject: Re: [swift-evolution] Working with enums by name
>>
>> Sorry, must've missed that.
>>
>>> On Jun 1, 2016, at 12:17 PM, Brent Royal-Gordon <brent at architechies.com>
>>
>> wrote:
>>>
>>>
>>>> This is, however, kind of a hack IMHO that relies on the compiler
>>
>> behavior that isn't well documented.
>>>
>>>
>>> It's documented in "The Swift Programming Language", in the same
>>
>> paragraphs where the `enum Planet` example we've been working with comes
>> from.
>>>
>>>
>>> “When you’re working with enumerations that store integer or string raw
>>
>> values, you don’t have to explicitly assign a raw value for each case.
>> When
>> you don’t, Swift will automatically assign the values for you.
>>>
>>> <snip>
>>> “When strings are used for raw values, the implicit value for each case
>>
>> is the text of that case’s name.”
>>>
>>>
>>> --
>>> Brent Royal-Gordon
>>> Architechies
>>>
>>
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>
>>
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>
>


More information about the swift-evolution mailing list