<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"><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=""><div class="">If the associated values are all enums themselves, that's great. What if your enum is an error type, and the associated values are all strings with maybe diagnostic info or time stamps or something?</div><div class=""><br class=""></div><div class="">If Swift’s stdlib had a protocol (either `Initable` or `HasDefaultValue`, as used below), and the language had better support for generic tuple processing:</div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">Tuples automatically conform to any protocol to which all its members conform.</div><div class="">Mapping over a tuple’s member’s types: <span style="font-family: Menlo; font-size: 11px; color: rgb(187, 44, 162);" class="">let</span><span style="font-family: Menlo; font-size: 11px;" class=""> x = (</span><span style="font-family: Menlo; font-size: 11px; color: rgb(112, 61, 170);" class="">Int</span><span style="font-family: Menlo; font-size: 11px;" class="">, </span><span style="font-family: Menlo; font-size: 11px; color: rgb(112, 61, 170);" class="">String</span><span style="font-family: Menlo; font-size: 11px;" class="">, </span><span style="font-family: Menlo; font-size: 11px; color: rgb(112, 61, 170);" class="">Double</span><span style="font-family: Menlo; font-size: 11px;" class="">)</span><span style="font-family: Menlo; font-size: 11px;" class="">.</span><span style="font-family: Menlo; font-size: 11px; color: rgb(61, 29, 129);" class="">map</span><span style="font-family: Menlo; font-size: 11px;" class=""> { $0</span><span style="font-family: Menlo; font-size: 11px;" class="">.</span><span style="font-family: Menlo; font-size: 11px; color: rgb(187, 44, 162);" class="">init</span><span style="font-family: Menlo; font-size: 11px;" class="">() </span><span style="font-family: Menlo; font-size: 11px;" class="">}</span><span style="font-family: Menlo; font-size: 11px;" class=""> </span><span style="color: rgb(0, 132, 0); font-family: Menlo; font-size: 11px;" class="">// returns (Int.init(), String.init(), Double.init())</span></div><div class="">Mapping over a tuple’s members: <span style="font-family: Menlo; font-size: 11px; color: rgb(187, 44, 162);" class="">let</span><span style="font-family: Menlo; font-size: 11px;" class=""> x = (</span><span style="font-family: Menlo; font-size: 11px; color: rgb(39, 42, 216);" class="">0</span><span style="font-family: Menlo; font-size: 11px;" class="">, </span><font color="#d12f1b" face="Menlo" class=""><span style="font-size: 11px;" class="">“”</span></font><font face="Menlo" class=""><span style="font-size: 11px;" class="">, </span></font><span style="font-family: Menlo; font-size: 11px; color: rgb(39, 42, 216);" class="">0.0</span><span style="font-family: Menlo; font-size: 11px;" class="">)</span><span style="font-family: Menlo; font-size: 11px;" class="">.</span><span style="font-family: Menlo; font-size: 11px; color: rgb(61, 29, 129);" class="">map</span><span style="font-family: Menlo; font-size: 11px;" class=""> { $0.</span><span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">hashValue </span><span style="font-family: Menlo; font-size: 11px;" class="">}</span><span style="font-family: Menlo; font-size: 11px;" class=""> </span><font color="#008400" face="Menlo" class=""><span style="font-size: 11px;" class="">// returns (0.hashValue, “”.hashValue, 0.0.hashValue)</span></font></div><div class="">Subscripting tuple’s: <span style="font-family: Menlo; font-size: 11px;" class="">(</span><span style="font-family: Menlo; font-size: 11px; color: rgb(39, 42, 216);" class="">0</span><span style="font-family: Menlo; font-size: 11px;" class="">, </span><font color="#d12f1b" face="Menlo" class=""><span style="font-size: 11px;" class="">“”</span></font><font face="Menlo" class=""><span style="font-size: 11px;" class="">, </span></font><span style="font-family: Menlo; font-size: 11px; color: rgb(39, 42, 216);" class="">0.0</span><span style="font-family: Menlo; font-size: 11px;" class="">)[</span><span style="color: rgb(39, 42, 216); font-family: Menlo; font-size: 11px;" class="">0</span><span style="font-family: Menlo; font-size: 11px;" class="">] </span></div><div class="">The ability to append/appendElements to tuples, such that if <span style="font-family: Menlo; font-size: 11px;" class="">T = (</span><span style="font-family: Menlo; font-size: 11px; color: rgb(112, 61, 170);" class="">Int</span><span style="font-family: Menlo; font-size: 11px;" class="">, </span><span style="font-family: Menlo; font-size: 11px; color: rgb(112, 61, 170);" class="">Int</span><span style="font-family: Menlo; font-size: 11px;" class="">), </span><span style="font-family: Menlo; font-size: 11px;" class="">U = (</span><span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">Double</span><span style="font-family: Menlo; font-size: 11px;" class="">, </span><span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">Double</span><span style="font-family: Menlo; font-size: 11px;" class="">)</span>, then <span style="font-family: Menlo; font-size: 11px;" class="">().appendElements(</span><span style="font-family: Menlo; font-size: 11px; color: rgb(79, 129, 135);" class="">T</span><span style="font-family: Menlo; font-size: 11px;" class="">,</span><span style="font-family: Menlo; font-size: 11px; color: rgb(79, 129, 135);" class="">U</span><span style="font-family: Menlo; font-size: 11px;" class="">).append(</span><span style="font-family: Menlo; font-size: 11px; color: rgb(112, 61, 170);" class="">NSFileManager</span><span style="font-family: Menlo; font-size: 11px;" class="">)</span> would equal <span style="font-family: Menlo; font-size: 11px;" class="">(</span><span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">Int</span><span style="font-family: Menlo; font-size: 11px;" class="">, </span><span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">Int</span><span style="font-family: Menlo; font-size: 11px;" class="">, </span><span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">Double</span><span style="font-family: Menlo; font-size: 11px;" class="">, </span><span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">Double</span><span style="font-family: Menlo; font-size: 11px;" class="">, </span><span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">NSFileManager</span><span style="font-family: Menlo; font-size: 11px;" class="">)</span> </div></blockquote>Then I think we’d only need a couple more features:<br class=""><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">An implicit typealias in enums, which is a tuple of all the cases’ associated value’s type</div><div class="">The ability to restrict a protocol to only be conformable by enums, like we can already do with classes</div></blockquote><div class=""><br class=""></div><div class="">and I think we’d get all this for free with a couple protocols:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">protocol</span> Initable { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span>() }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">protocol</span> HasDefaultValue {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">typealias</span> DefaultValue</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">static</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> defaultValue: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">DefaultValue</span> {<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">get</span>}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">extension</span> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">HasDefaultValue</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">where</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">Self</span>: Initable, DefaultValue == <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">Self</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">static</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> defaultValue: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">DefaultValue</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> { </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">Self</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">() } </span>// can be overridden if not appropriate</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">protocol</span> FiniteEnumerable : <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">enum</span>, <span style="color: rgb(79, 129, 135);" class="">HasDefaultValue</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">typealias</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> AssociatedValues </span>// A tuple of the associated type for each case</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">typealias</span> DefaultValue</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">static</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> defaultValues: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">DefaultValue</span> {<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">get</span>}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">static</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> cases: [<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Self</span>] {<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">get</span>}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>FiniteEnumerable<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">static</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> count: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Mirror</span>(reflecting: <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.defaultValues).<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">children</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">count</span>) }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">extension</span> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">FiniteEnumerable</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">where</span> AssociatedValues: HasDefaultValue, DefaultValue == AssociatedValues.DefaultValue {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color: rgb(187, 44, 162);" class="">static</span> <span style="color: rgb(187, 44, 162);" class="">var</span> defaultValues: <span style="color: rgb(112, 61, 170);" class="">DefaultValue</span> { <span style="color: rgb(187, 44, 162);" class="">return</span> AssociatedValues.defaultValue }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">extension</span> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">FiniteEnumerable</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">where</span> AssociatedValues: FiniteEnumerable, DefaultValue == [<span style="color: rgb(187, 44, 162);" class="">Self</span>] {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">static</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> defaultValues: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">DefaultValue</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> allCases = ()</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">for</span> i <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span> 0 ..< <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">Self</span>.cases.count {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">for</span> assocValue <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span> AssociatedValues.cases[i] {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> allCases.append(theCase(assocValue)) </span>// FWIW, this line and the “FiniteEnumerable : enum” parts are the only two errors Xcode can find.</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> allCases</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> } </span>// Over</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div><div class=""><br class=""></div><div class="">Hmm… I may have gotten `defaultValues` and `cases` switched around in there. But the point is that they all could be added through protocol extensions, which have the advantage of:</div><div class="">1) provides default declarations</div><div class="">2) that are overridable if you don’t like them</div><div class="">3) it’ll throw a compile-time nonconformance error if there’s a hole in the protocol extensions</div><div class="">4) less magic.</div><div class=""><br class=""></div><div class="">- Dave Sweeris</div><div class=""><br class="">On Jan 20, 2016, at 10:04, Joe Groff via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><meta http-equiv="Content-Type" content="text/html charset=us-ascii" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Jan 20, 2016, at 9:01 AM, Jacob Bandes-Storch via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">I've been thinking about naming a bit. I'm worried that "values" / "allValues" won't make sense if this feature eventually goes in the direction of returning type constructors. For enums with associated types, the cases may actually be <b class="">functions</b> taking the associated type and returning the enum type. (There's an example under "Future directions" in the proposal). Calling these "values" seems strange to me, but calling them "cases" makes sense.</div></div></blockquote><div class=""><br class=""></div><div class="">That seems like a fairly specialized use case to me. I'd expect the default behavior for enums with associated values to enumerate all possible values of each case (whose associated values would themselves need to be ValueEnumerable), not the case constructors themselves. That's more generally useful when using associated values to categorize constants.</div><div class=""><br class=""></div><div class="">-Joe</div></div></div></blockquote></body></html>