On 08.09.2017 12:56, Logan Shire via swift-evolution wrote:
Googling ‘swift iterate over enum cases’ yields many results of various levels 
of hackery.
Obviously it’s trivial to write a computed property that returns an enum’s 
cases as an
array, but maintaining that is prone to error. If you add another case, you 
need to make sure
you update the array property. For enums without associated types,
I propose adding a synthesized static var, ‘cases', to the enum’s type. E.g.

Yes, this feature was discussed previously, but was not going into formal review for different reasons. Personally, I want this feature very much. Currently, as I understand, if someone will implement this feature in Swift and raised a formal review - this addition can be accepted. So, if someone is ready to implement this in Swift compiler code, we can discuss details(again) in this pitch to prepare a formal proposal.

Vladimir.


enum Suit: String {
     case spades = "♠"
     case hearts = "♥"
     case diamonds = "♦"
     case clubs = "♣"
}

let values = (1…13).map { value in
     switch value {
     case 1: return “A”
     case 11: return “J”
     case 12: return “Q”
     case 13: return “K”
     default: return String(value)
     }
}

let cards = values.flatMap { value in Suit.cases.map { “\($0)\(value)"  } }

Yields [“♠A”, “ ♥ A”, …, “♣K”]
Thoughts?


Thanks!
- Logan Shire
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to