<html><body><div><br></div><div>Perhaps if enums were automatically indexed by the compiler&nbsp;for&nbsp;the order of the case declerations?</div><div><br data-mce-bogus="1"></div><div>enum Food {</div><div>&nbsp;&nbsp;&nbsp;case Fruit</div><div>&nbsp;&nbsp;&nbsp;case Vegetable</div><div>&nbsp;&nbsp;&nbsp;case Meat</div><div>}<br><span class="body-text-content"><br>Food.Fruit.index&nbsp;== 0</span></div><div><span class="body-text-content">Food.Vegetable.index == 1</span></div><div><span class="body-text-content">Food.Meat.index ==&nbsp;2<br><br><br> <strong>3. Re: Proposal: Enum 'count' functionality (Joe Groff)</strong><br><br><br>Message: 3<br>Date: Tue, 22 Dec 2015 07:24:37 -0800<br>From: Joe Groff &lt;jgroff@apple.com&gt;<br>To: Zef Houssney &lt;zefmail@gmail.com&gt;<br>Cc: Swift Evolution &lt;swift-evolution@swift.org&gt;<br>Subject: Re: [swift-evolution] Proposal: Enum 'count' functionality<br>Message-ID: &lt;7BA6F3E4-1053-4EBB-BAEB-DF5D00A816BC@apple.com&gt;<br>Content-Type: text/plain; charset=utf-8<br><br><br><blockquote type="cite" class="quoted-plain-text">On Dec 21, 2015, at 10:33 PM, Zef Houssney &lt;zefmail@gmail.com&gt; wrote:</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">I agree with Stephen Celis that the best names for this (yet) are definitely `cases` and optionally `rawValues` if the cases are RawRepresentable.</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">Regarding handling cases with associated values where some of those values are other enums, it seems odd to me to try to return every variation of each case. I can’t picture how that would work sanely when there are multiple associated values, such as a case like `MyCase(String, SomeEnum)`. Therefore I prefer the idea of having the array contain the constructor for cases with associated values. The main problem I see here is that the type system (as far as I know) can’t fully represent that type of thing, but I think it’s still potentially valuable. Related to this I’m putting together a separate proposal that explores a bit more on why I believe the enum constructor to be valuable, though that’s in a bit of a different context.</blockquote><br>Generating a list of values for MyCase(String, SomeEnum) wouldn't make sense because there are an infinite number of Strings, so Strings don't make sense as a value-enumerable type. However, you could have a mostly-flat enum that uses subenums for organization:<br><br>enum Food {<br> enum Fruit {<br> case Apple, Banana, BellPepper<br> }<br> case fruit(Fruit)<br><br> enum Meat {<br> case Beef, Pork, Venison<br> }<br> case meat(Meat)<br>}<br><br>In many C family code bases it's common to group related enums by order, even if there's otherwise no intrinsic order among the elements. Preserving the structure of the cases in the type system makes them easier to understand and work with in pattern-matching, since you can handle all fruits by matching `.fruit(_)` instead of having to maintain `food &gt;= FirstFruit &amp;&amp; foo &lt;= LastFruit` ranges. For these kinds of enums, recursively generating the associated values is still useful.<br><br>-Joe<br><br><blockquote type="cite" class="quoted-plain-text">I shared these thoughts in a bit more detail here a couple weeks ago, with some followup in the next two messages:</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text"><a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/001864.html" data-mce-href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/001864.html">https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/001864.html</a></blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">Zef</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text">On Dec 21, 2015, at 3:58 PM, Erica Sadun via swift-evolution &lt;swift-evolution@swift.org&gt; wrote:</blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><br></blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text">That would be okay too. Thank you, Santa Joe.</blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><br></blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text">-- E, who hopes she was on the nice list.</blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><br></blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text">On Dec 21, 2015, at 3:56 PM, Joe Groff &lt;jgroff@apple.com&gt; wrote:</blockquote></blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><br></blockquote></blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><br></blockquote></blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text">On Dec 21, 2015, at 12:24 PM, Erica Sadun &lt;erica@ericasadun.com&gt; wrote:</blockquote></blockquote></blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><br></blockquote></blockquote></blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text">I could be satisfied by such an approach. I could even be more satisfied if</blockquote></blockquote></blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><br></blockquote></blockquote></blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text">enum Foo: ValueEnumerable { case A, B, C }</blockquote></blockquote></blockquote></blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><br></blockquote></blockquote></blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><br></blockquote></blockquote></blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text">were also essentially an alias for</blockquote></blockquote></blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><br></blockquote></blockquote></blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text">enum Foo: Int, ValueEnumerable { case A=0, B, C }</blockquote></blockquote></blockquote></blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><br></blockquote></blockquote></blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text">:)</blockquote></blockquote></blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><br></blockquote></blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text">If the value collection were sufficiently capable, you wouldn't necessarily need an implicit Int raw value; you could do `allValues.indexOf(.A)` to get the index for a case.</blockquote></blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><br></blockquote></blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text">-Joe</blockquote></blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><br></blockquote></blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><br></blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text">_______________________________________________</blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text">swift-evolution mailing list</blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><a href="mailto:swift-evolution@swift.org" data-mce-href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><blockquote type="cite" class="quoted-plain-text"><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" data-mce-href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></blockquote></blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><br><br><br><br>------------------------------<br><br>_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" data-mce-href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" data-mce-href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br><br><br>End of swift-evolution Digest, Vol 1, Issue 227<br>***********************************************<br></span></div><div><blockquote type="cite"><div class="msg-quote"></div></blockquote></div></body></html>