[swift-evolution] Working with enums by name

Leonardo Pessoa me at lmpessoa.com
Tue May 31 20:26:00 CDT 2016


Don't you think it's a bit of a waste to be repeating the name of the value as a string just to use init(rawValue:) with them? What if I need to store another string associated with the value of the enum e.g. I want to create an enum to represent options in a menu and the associated value is to be the name of the image file to be used for that option? I don't know how common that is, but I don't see how good it is for you to keep repeating yourself in your code when the value you want and need is right by your side (DRY principle) and can be used easily in one way (to string) but not the other (back to enum).

L

On 31 May 2016, at 6:48 pm, Brent Royal-Gordon <brent at architechies.com> wrote:

>> |   enum Planet : Int {
>> |      case Mercury = 1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune
>> |
>> |      init?(caseName name : String) {
> 
> The compiler actually does this already through RawRepresentable if you put `String` as your raw type. So what's the use case for this? Code which needs both a non-String rawValue *and* needs to look up cases by name? How common do you think that is?
> 
> -- 
> Brent Royal-Gordon
> Architechies
> 


More information about the swift-evolution mailing list