[swift-evolution] Working with enums by name
Vladimir.S
svabox at gmail.com
Wed Jun 1 11:21:59 CDT 2016
This will not print the name of case :
enum E: CustomStringConvertible {
case one, two
var description: String {return "haha"}
}
print(E.one)
So, for me, it seems like the good idea to have a standard(and built-in)
way to convert string<->case i.e. to have
let e = E(caseName: "one")!
and
let s = e.caseName // always the same as defined in enum type
On 01.06.2016 18:47, Leonardo Pessoa via swift-evolution wrote:
> Paul, in all my tests for this thread printing the enum value only
> produced the enum value's name ("Mars" in your example). The proposal
> of having a .caseName (or should it better be .caseValue to cover
> enums with associated values? any other suggestions?) will prevent
> that changes to this behaviour crash apps in the future as this should
> always produce the same result even if the string representation
> changes.
>
> L
>
> On 1 June 2016 at 12:15, Paul Cantrell via swift-evolution
> <swift-evolution at swift.org> wrote:
>> IIRC, string interpolation prepends the module name if the enum belongs to a module: “MyLib.Mars” instead of just “Mars”. It’s also been a source of compiler crashes, at least in the past.
>>
>> Those two factors forced me into this ugliness: https://github.com/bustoutsolutions/siesta/blob/master/Source/ResourceObserver.swift#L106-L115
>>
>> A clean, documented, supported way of exposing the enum case name that the runtime clearly already has available seems sensible — and should be independent of the raw type.
>>
>> Cheers, P
>>
>>> On Jun 1, 2016, at 5:10 AM, Charlie Monroe via swift-evolution <swift-evolution at swift.org> wrote:
>>>
>>> This is, however, kind of a hack IMHO that relies on the compiler behavior that isn't well documented.
>>>
>>> For example, this:
>>>
>>> enum Planet {
>>> case Earth
>>> case Mars
>>> }
>>>
>>> "\(Planet.Mars)" // This is "Mars"
>>>
>>>
>>> Works as well. You don't need to have the represented value to be String.
>>>
>>> Note that this:
>>>
>>> - works both when you have a plain enum, or enum Planet: Int, or whatever raw value kind
>>> - does not work (!) when declared as @objc - then the result is "Planet".
>>>
>>>> On Jun 1, 2016, at 9:52 AM, Patrick Smith via swift-evolution <swift-evolution at swift.org> wrote:
>>>>
>>>> I had no idea you could do this!!
>>>>
>>>>> On 1 Jun 2016, at 12:32 PM, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org> wrote:
>>>>>
>>>>> Who said anything about repeating the name?
>>>>>
>>>>> Welcome to Apple Swift version 2.2 (swiftlang-703.0.18.8 clang-703.0.30). Type :help for assistance.
>>>>> 1> enum Planet: String { case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune }
>>>>> 2> Planet.mercury.rawValue
>>>>> $R0: String = "mercury"
>>>>
>>>> _______________________________________________
>>>> 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
>>
>> _______________________________________________
>> 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