<div dir="ltr"><div># Summary</div><div>Add an allValues function to all enums that are RawRepresentable to access, iterate over, and count all of the values of an enum. The usage is general enough that I believe it would be a great addition to the language. </div><div><div><br></div><div>This can currently be "hacked" in a few different ways by using the RawRepresentable initializer (SO <a href="http://stackoverflow.com/questions/24007461/how-to-enumerate-an-enum-with-string-type/24137319#24137319">link</a>). </div></div><div><br></div><div>I'm presenting this idea here for discussion and feedback before I write up a formal proposal. </div><div><br></div><div># Limits</div><div>Having an allValues method would only be logical for enums which do not have associated values. This limits the enum to RawRepresentable enums and basic enums (is there a name for an enum without associated values and no raw value?). </div><div>I'm working with the assumption that this would only be done with RawRepresentable enums but it may be the case that basic enums can be included easily.</div><div><br></div><div># Examples</div><div>All examples as source code are available as a <a href="https://gist.github.com/kevinrandrup/00448c37ae9c20fa4eab">gist</a>.</div><div><br></div><div>enum CommandLineFlag : String {<br></div><div> case Version = "--version"</div><div> case Help = "--help"</div><div> case Start = "--start"</div><div>}<br></div><div><br></div><div><div>func displayHelp() {</div><div> print("Available flags\n")</div><div> for flag in CommandLineFlag.allValues {</div><div> print("\(flag): \(flag.rawValue)")</div><div> }</div><div>}</div></div><div><br></div><div><br></div><div><br></div><div>Representing the structure and implementing UITableViewDataSource methods</div><div><div>enum RecipeTableViewSection : Int {</div><div> case Header</div><div> case Details</div><div>}</div><div><br></div><div>enum RecipeHeaderRow : Int {</div><div> case Name</div><div> case Image</div><div>}</div><div><br></div><div>enum RecipeDetailRow : Int {</div><div> case Ingredients</div><div> case Cost</div><div> case PreparationTime</div><div>}</div></div><div><br></div><div>// UITableViewDataSource implementation</div>func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {<br><div> switch RecipeTableViewSection(rawValue: section)! {<br> case .Header:<br> return RecipeHeaderRow.allValues().count<br> case .Details:<br> return RecipeDetailRow.allValues().count<br> }<br>}<br><br>func numberOfSectionsInTableView(tableView: UITableView) -> Int {<br> return RecipeTableViewSection.allValues().count<br><div><div>}</div></div><div><br></div><div># Decisions/Questions</div><div><br></div><div>1. Function vs. computed property vs. stored static variable<br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div>static func allValues() -> [CommandLineFlag] {</div><div> return [Version, Help, Start]</div><div>}</div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><br></div><div>static var allValues: [CommandLineFlag] {</div><div> return [Version, Help, Start]</div><div>}</div><div><br></div><div>static let allValues = [Version, Help, Start]</div></blockquote><ul><li>Currently leaning towards computed property</li><ul><li>Computed property > function - allValues doesn't do anything besides return a value so it doesn't need to be a function</li><li>Computed property > stored static variable - Will not increase the memory usage of the program<br></li></ul><li>A change between computed and stored static property would not be a source breaking change if it turns out one is better than the other.</li></ul><div>2. Set vs. Array<br></div><div><ul><li>Set - There are no duplicate values and RawRepresentable enums already conform to Hashable/Equatable.</li><li>Array - Preserves the cases' declaration order</li></ul></div><div>3. Should allValues consist of the enum type or RawValue? (CommandLineFlag vs. String)<br></div><div><ul><li>Strongly learning towards enum type</li><li>However, either one could be converted to the other and one could simply be a computed property of the other.</li></ul></div><div><div class="gmail_signature"><div dir="ltr"><br></div><div>If you have better examples and use cases, I would love to hear about them before writing the proposal.</div><div dir="ltr"><br></div><div dir="ltr"> - Kevin Randrup</div></div></div>
</div></div>