<div dir="ltr">Please see existing discussion at <a href="https://github.com/apple/swift-evolution/pull/114">https://github.com/apple/swift-evolution/pull/114</a>.<div><br></div><div>As discussed on <a href="https://github.com/apple/swift-evolution/pull/199">#199</a>, I believe we should continue discussing this (preferably on the same thread as the original, or a new one that references it), but I'm currently in the middle of a move and multiple travel plans, so I can't spearhead this for another week or two.</div><div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature"><div dir="ltr"><div>Jacob<br></div></div></div></div>
<br><div class="gmail_quote">On Thu, Mar 24, 2016 at 2:23 PM, Kevin Randrup via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><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" target="_blank">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" target="_blank">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><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><span class=""><font color="#888888"><div dir="ltr"><br></div><div dir="ltr"> - Kevin Randrup</div></font></span></div></div>
</div></div>
<br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div></div></div>