<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">+1<div class=""><br class=""></div><div class="">Finally, let’s get it done</div><div class=""><br class=""></div><div class=""><div><blockquote type="cite" class=""><div class="">On 4 Jul 2016, at 03:36, Gabriel Lanata via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_quote"><div dir="ltr" class=""><div class="gmail_quote"><div dir="ltr" class=""><div class="gmail_quote"><div dir="ltr" class=""><div class="">Hello, this has been proposed multiple times before in different ways.&nbsp;</div><div class=""><div class=""><font color="#333333" class=""><br class="">Link to detailed proposal draft:</font></div><a href="https://github.com/gabriellanata/swift-evolution/blob/master/proposals/NNNN-allow-enumerating-cases-in-enumerations.md" target="_blank" class="">https://github.com/gabriellanata/swift-evolution/blob/master/proposals/NNNN-allow-enumerating-cases-in-enumerations.md</a></div><div class=""><br class=""></div><div class="">It is currently not possible to enumerate through all the possible cases of an enumeration object without adding a hardcoded value or recurring to unsafe workarounds. The proposal is to add a method of obtaining an array or set of all the possible cases natively for enumerations without associated values.</div><div class=""><br class=""></div><div class=""><div class="">The proposed solution is to i<span style="line-height:1.5" class="">mplement a native `.cases` static var for all enumerations without associated&nbsp;</span><span style="line-height:1.5" class="">values. This would provide a strict type-safe way of enumerating though the&nbsp;</span><span style="line-height:1.5" class="">possible cases without risking runtime errors. The resulting code is a lot&nbsp;</span><span style="line-height:1.5" class="">cleaner and less prone to errors caused by forgetting to update the hardcoded&nbsp;</span><span style="line-height:1.5" class="">values when modifying cases.</span></div><div class=""><br class=""></div><div class=""><div class=""><div class="">Resulting code:</div><div class="">&nbsp;</div></div></div><div class="">&nbsp; &nbsp;enum PokemonType {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;case Fire, Grass, Water, Wind</div><div class="">&nbsp; &nbsp;}</div><div class="">&nbsp; &nbsp;for pokemonType in PokemonType.cases {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;// Do stuff</div><div class="">&nbsp; &nbsp;}</div><div class="">&nbsp; &nbsp;PokemonType.cases.count // Returns 4</div></div><div class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">-------</div><div class="">Gabriel</div><div class=""><br class=""></div></div></div></div></div></div></div></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></body></html>