Swift: text of enum value names should be easily accessible

Originator:garth
Number:rdar://22057107 Date Originated:29-Jul-2015 12:51 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Swift 2
Classification:Enhancement Reproducible:Not Applicable
 
This verges on the hackish and ad-hoc, but it would be so freakin’ useful…

I find, just as a matter of practical experience, that I’m constantly writing  utility routines of the following form:

enum MyTerm { … }

extension MyTerm {
    var description: String {
        switch self {
            case .Script:    return "script"
            case .Fraction:  return "fraction"
            case .Exponent:  return "exponent"
        }
    }
}

I see this all the time in other people’s code, too, so I know it’s not just me.

Enums with a raw-value type of String automatically use their case name as the raw value, which is helpful. But it doesn’t cover situations where you want to use the raw value for some other purpose, or situations where cases may have arguments of their own (since no raw values are then allowed).

It would be very helpful if Swift supplied a caseName property for all enum instances.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!