<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Sun, Jan 17, 2016 at 5:56 PM, Rod Brown <span dir="ltr">&lt;<a href="mailto:rodney.brown6@icloud.com" target="_blank">rodney.brown6@icloud.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">While this isn’t in review yet, massive +1 from me. Good first implementation, and very welcome functionality.<div><br></div><div>Personally, I’m actually dealing with the difficulties of counts and enumerating enums in Obj-C and this is a pain in the neck. It’s not difficult, but it’s prone to breakage when someone alters code.</div></div></blockquote><div><br></div><div>If you&#39;re willing to share, I think it would help to have more examples of what problems people are trying to solve.</div><div><br></div><div>Jacob</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><br></div><div>This would be a vast improvement.</div><div><br></div><div><br><div><blockquote type="cite"><div><div class="h5"><div>On 18 Jan 2016, at 10:44 AM, Jacob Bandes-Storch via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br></div></div><div><div><div class="h5"><div dir="ltr"><div><div><div dir="ltr"><div><span style="font-size:12.8px">Hi folks,</span><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">I&#39;ve drafted a proposal to add a CaseEnumerable protocol, which will derive a static variable &quot;cases&quot; for enum types. Feedback is welcome, especially for refining the proposal before I submit a formal PR.</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">The draft is here; full text below. <a href="https://github.com/jtbandes/swift-evolution/blob/977a9923fd551491623b6bfd398d5859488fe1ae/proposals/0000-derived-collection-of-enum-cases.md" target="_blank">https://github.com/jtbandes/swift-evolution/blob/977a9923fd551491623b6bfd398d5859488fe1ae/proposals/0000-derived-collection-of-enum-cases.md</a></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><br clear="all"><div><div dir="ltr"><h1 style="font-size:2.25em;margin-right:0px;margin-bottom:16px;margin-left:0px;line-height:1.2;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;margin-top:0px!important">Derived Collection of Enum Cases</h1><ul style="padding:0px 0px 0px 2em;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px"><li style="margin-left:15px">Proposal: <a href="https://github.com/apple/swift-evolution/blob/master/proposals/NNNN-derived-collection-of-enum-cases.md" style="color:rgb(64,120,192);text-decoration:none;background-color:transparent" target="_blank">SE-NNNN</a></li><li style="margin-left:15px">Author(s): <a href="https://github.com/jtbandes" style="color:rgb(64,120,192);text-decoration:none;background-color:transparent" target="_blank">Jacob Bandes-Storch</a></li><li style="margin-left:15px">Status: <strong>Awaiting review</strong></li><li style="margin-left:15px">Review manager: TBD</li></ul><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;"><a href="https://github.com/jtbandes/swift-evolution/blob/977a9923fd551491623b6bfd398d5859488fe1ae/proposals/0000-derived-collection-of-enum-cases.md#introduction" style="color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1;background-color:transparent" target="_blank"><span style="font-weight:normal;font-size:16px;line-height:1;font-family:octicons;display:inline-block;vertical-align:middle"></span></a>Introduction</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px">It is a truth universally acknowledged, that a programmer in possession of an enum with many cases, must eventually be in want of dynamic enumeration over them.</p><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px">This topic has come up three times on the swift-evolution mailing list so far:</p><ul style="padding:0px 0px 0px 2em;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px"><li style="margin-left:15px"><a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/001233.html" style="color:rgb(64,120,192);text-decoration:none;background-color:transparent" target="_blank">List of all Enum values (for simple enums)</a> (December 8, 2015)</li><li style="margin-left:15px"><a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151221/003819.html" style="color:rgb(64,120,192);text-decoration:none;background-color:transparent" target="_blank">Proposal: Enum &#39;count&#39; functionality</a> (December 21, 2015)</li><li style="margin-left:15px"><a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160111/006853.html" style="color:rgb(64,120,192);text-decoration:none;background-color:transparent" target="_blank">Draft Proposal: count property for enum types</a> (January 17, 2016)</li></ul><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px">Enumerating enumerations in Swift is also a popular topic on Stack Overflow:</p><ul style="padding:0px 0px 0px 2em;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px"><li style="margin-left:15px"><a href="http://stackoverflow.com/questions/24007461/how-to-enumerate-an-enum-with-string-type" style="color:rgb(64,120,192);text-decoration:none;background-color:transparent" target="_blank">How to enumerate an enum with String type?</a> (June 3, 2014; question score 131)</li><li style="margin-left:15px"><a href="http://stackoverflow.com/questions/27094878/how-do-i-get-the-count-of-a-swift-enum" style="color:rgb(64,120,192);text-decoration:none;background-color:transparent" target="_blank">How do I get the count of a Swift enum?</a> (November 23, 2014; question score 37)</li></ul><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;"><a href="https://github.com/jtbandes/swift-evolution/blob/977a9923fd551491623b6bfd398d5859488fe1ae/proposals/0000-derived-collection-of-enum-cases.md#motivation" style="color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1;background-color:transparent" target="_blank"><span style="font-weight:normal;font-size:16px;line-height:1;font-family:octicons;display:inline-block;vertical-align:middle"></span></a>Motivation</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px">Simple enums are finite, and their values are statically known to the compiler, yet working with them programmatically is challenging. It is often desirable to iterate over all possible cases of an enum, or to know the number of cases (or maximum valid rawValue).</p><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px">Currently, however, there is no built-in reflection or enumeration support. Users must resort to manually listing out cases in order to iterate over them:</p><div style="margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px"><pre style="white-space:pre-wrap;overflow:auto;font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;font-stretch:normal;line-height:1.45;padding:16px;border-radius:3px;word-wrap:normal;background-color:rgb(247,247,247)"><span style="color:rgb(167,29,93)">enum</span> Attribute {
    <span style="color:rgb(167,29,93)">case</span> Date, Name, Author
}
<span style="color:rgb(167,29,93)">func</span> <span style="color:rgb(121,93,163)">valueForAttribute</span>(attr: Attribute) <span style="color:rgb(167,29,93)">-&gt;</span> <span style="color:rgb(0,134,179)">String</span> { …from elsewhere… }

<span style="color:rgb(150,152,150)">// Cases must be listed explicitly:</span>
[Attribute<span style="color:rgb(167,29,93)">.</span>Date, <span style="color:rgb(167,29,93)">.</span>Name, <span style="color:rgb(167,29,93)">.</span>Author]<span style="color:rgb(167,29,93)">.</span><span style="color:rgb(0,134,179)">map</span>{ valueForAttribute($<span style="color:rgb(0,134,179)">0</span>) }<span style="color:rgb(167,29,93)">.</span>joinWithSeparator(<span style="color:rgb(24,54,145)">&quot;\n&quot;</span>)</pre></div><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px">For RawRepresentable enums, users have often relied on iterating over the known (or assumed) allowable raw values:</p><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px"><em>Annotated excerpt from Nate Cook&#39;s post, <a href="http://natecook.com/blog/2014/10/loopy-random-enum-ideas/" style="color:rgb(64,120,192);text-decoration:none;background-color:transparent" target="_blank">Loopy, Random Ideas for Extending &quot;enum&quot;</a> (October 2014):</em></p><div style="margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px"><pre style="white-space:pre-wrap;overflow:auto;font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;font-stretch:normal;line-height:1.45;padding:16px;border-radius:3px;word-wrap:normal;background-color:rgb(247,247,247)"><span style="color:rgb(167,29,93)">enum</span> Reindeer: <span style="color:rgb(0,134,179)">Int</span> {
    <span style="color:rgb(167,29,93)">case</span> Dasher, Dancer, Prancer, Vixen, Comet, Cupid, Donner, Blitzen, Rudolph  
}
<span style="color:rgb(167,29,93)">extension</span> Reindeer {
    <span style="color:rgb(167,29,93)">static</span> <span style="color:rgb(167,29,93)">var</span> allCases: [Reindeer] {
        <span style="color:rgb(167,29,93)">var</span> cur <span style="color:rgb(167,29,93)">=</span> <span style="color:rgb(0,134,179)">0</span>
        <span style="color:rgb(167,29,93)">return</span> <span style="color:rgb(0,134,179)">Array</span>(
            GeneratorOf<span style="color:rgb(167,29,93)">&lt;</span>Reindeer<span style="color:rgb(167,29,93)">&gt;</span> {
                <span style="color:rgb(167,29,93)">return</span> Reindeer(rawValue: cur<span style="color:rgb(167,29,93)">++</span>)
            }
        )
    }
    <span style="color:rgb(167,29,93)">static</span> <span style="color:rgb(167,29,93)">var</span> caseCount: <span style="color:rgb(0,134,179)">Int</span> {
        <span style="color:rgb(167,29,93)">var</span> <span style="color:rgb(0,134,179)">max</span>: <span style="color:rgb(0,134,179)">Int</span> <span style="color:rgb(167,29,93)">=</span> <span style="color:rgb(0,134,179)">0</span>
        <span style="color:rgb(167,29,93)">while</span> <span style="color:rgb(167,29,93)">let</span> _ <span style="color:rgb(167,29,93)">=</span> <span style="color:rgb(167,29,93)">self</span>(rawValue: <span style="color:rgb(167,29,93)">++</span><span style="color:rgb(0,134,179)">max</span>) {}
        <span style="color:rgb(167,29,93)">return</span> <span style="color:rgb(0,134,179)">max</span>
    }
    <span style="color:rgb(167,29,93)">static</span> <span style="color:rgb(167,29,93)">func</span> <span style="color:rgb(121,93,163)">randomCase</span>() <span style="color:rgb(167,29,93)">-&gt;</span> Reindeer {
        <span style="color:rgb(150,152,150)">// everybody do the Int/UInt32 shuffle!</span>
        <span style="color:rgb(167,29,93)">let</span> randomValue <span style="color:rgb(167,29,93)">=</span> <span style="color:rgb(0,134,179)">Int</span>(arc4random_uniform(<span style="color:rgb(0,134,179)">UInt32</span>(caseCount)))
        <span style="color:rgb(167,29,93)">return</span> <span style="color:rgb(167,29,93)">self</span>(rawValue: randomValue)<span style="color:rgb(167,29,93)">!</span>
    }
}</pre></div><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px">There are many problems with these existing techniques:</p><ul style="padding:0px 0px 0px 2em;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px"><li style="margin-left:15px">They are ad-hoc and can&#39;t benefit every enum type without duplicated and code.</li><li style="margin-left:15px">They are not standardized across codebases, nor provided automatically by libraries such as Foundation and {App,UI}Kit.</li><li style="margin-left:15px">They are sometimes prone to bugs when enum cases are added, but the user forgets to update a hard-coded static collection of cases.</li></ul><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;"><a href="https://github.com/jtbandes/swift-evolution/blob/977a9923fd551491623b6bfd398d5859488fe1ae/proposals/0000-derived-collection-of-enum-cases.md#precedent-in-other-languages" style="color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1;background-color:transparent" target="_blank"><span style="font-weight:normal;font-size:16px;line-height:1;font-family:octicons;display:inline-block;vertical-align:middle"></span></a>Precedent in other languages</h2><ul style="padding:0px 0px 0px 2em;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px"><li style="margin-left:15px"><p style="margin-top:16px;margin-bottom:16px">Rust does not seem to have a solution for this problem.</p></li><li style="margin-left:15px"><p style="margin-top:16px;margin-bottom:16px">C#&#39;s Enum has several <a href="https://msdn.microsoft.com/en-us/library/system.enum_methods.aspx" style="color:rgb(64,120,192);text-decoration:none;background-color:transparent" target="_blank">methods</a> available for reflection, including <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">GetValues()</code> and <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">GetNames()</code>.</p></li><li style="margin-left:15px"><p style="margin-top:16px;margin-bottom:16px">Java <a href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.9.3" style="color:rgb(64,120,192);text-decoration:none;background-color:transparent" target="_blank">implicitly declares</a> a static <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">values()</code> function, returning an array of enum values, and <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">valueOf(String name)</code> which takes a String and returns the enum value with the corresponding name (or throws an exception). More examples <a href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.9.3" style="color:rgb(64,120,192);text-decoration:none;background-color:transparent" target="_blank">here</a>.</p></li><li style="margin-left:15px"><p style="margin-top:16px;margin-bottom:16px">The Template Haskell extension to Haskell provides a function <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">reify</code> which extracts <a href="http://hackage.haskell.org/package/template-haskell-2.10.0.0/docs/Language-Haskell-TH-Syntax.html#t:Info" style="color:rgb(64,120,192);text-decoration:none;background-color:transparent" target="_blank">info about types</a>, including their constructors.</p></li></ul><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;"><a href="https://github.com/jtbandes/swift-evolution/blob/977a9923fd551491623b6bfd398d5859488fe1ae/proposals/0000-derived-collection-of-enum-cases.md#proposed-solution" style="color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1;background-color:transparent" target="_blank"><span style="font-weight:normal;font-size:16px;line-height:1;font-family:octicons;display:inline-block;vertical-align:middle"></span></a>Proposed solution</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px">Introduce a <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">CaseEnumerable</code> protocol. Conforming to CaseEnumerable will automagically derive a <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">static var cases</code>, whose type is a CollectionType of all the enum&#39;s values.</p><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px">Like <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">ErrorType</code>, the <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">CaseEnumerable</code> protocol will not have any user-visible requirements; merely adding the conformance is enough to enable case enumeration.</p><div style="margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px"><pre style="white-space:pre-wrap;overflow:auto;font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;font-stretch:normal;line-height:1.45;padding:16px;border-radius:3px;word-wrap:normal;background-color:rgb(247,247,247)"><span style="color:rgb(167,29,93)">enum</span> Ma { <span style="color:rgb(167,29,93)">case</span> 马, 吗, 妈, 码, 骂, 🐎, 🐴 }

<span style="color:rgb(167,29,93)">extension</span> Ma: CaseEnumerable {}

Ma<span style="color:rgb(167,29,93)">.</span>cases         <span style="color:rgb(150,152,150)">// returns some CollectionType whose Generator.Element is Ma</span>
Ma<span style="color:rgb(167,29,93)">.</span>cases<span style="color:rgb(167,29,93)">.</span><span style="color:rgb(0,134,179)">count</span>   <span style="color:rgb(150,152,150)">// returns 7</span>
<span style="color:rgb(0,134,179)">Array</span>(Ma<span style="color:rgb(167,29,93)">.</span>cases)  <span style="color:rgb(150,152,150)">// returns [Ma.马, .吗, .妈, .码, .骂, .🐎, .🐴]</span></pre></div><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px">Conformances can even be added for enums which are defined in other modules:</p><div style="margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px"><pre style="white-space:pre-wrap;overflow:auto;font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;font-stretch:normal;line-height:1.45;padding:16px;border-radius:3px;word-wrap:normal;background-color:rgb(247,247,247)"><span style="color:rgb(167,29,93)">extension</span> NSTextAlignment: CaseEnumerable {}

<span style="color:rgb(0,134,179)">Array</span>(NSTextAlignment<span style="color:rgb(167,29,93)">.</span>cases)  <span style="color:rgb(150,152,150)">// returns [NSTextAlignment.Left, .Right, .Center, .Justified, .Natural]</span></pre></div><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;"><a href="https://github.com/jtbandes/swift-evolution/blob/977a9923fd551491623b6bfd398d5859488fe1ae/proposals/0000-derived-collection-of-enum-cases.md#detailed-design" style="color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1;background-color:transparent" target="_blank"><span style="font-weight:normal;font-size:16px;line-height:1;font-family:octicons;display:inline-block;vertical-align:middle"></span></a>Detailed design</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px">Enum cases are enumerated in the order they appear in the source code.</p><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px">The <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">cases</code> collection does not necessitate <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Ω(number of cases)</code> static storage. For integer-backed enums, only the range(s) of valid rawValues need to be stored, and the enum construction can happen dynamically.</p><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px">Attempting to derive CaseEnumerable for a non-<code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">enum</code> type will result in a compiler error.</p><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px">Attempting to derive CaseEnumerable for an enum with associated values will result in a compiler error.</p><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;"><a href="https://github.com/jtbandes/swift-evolution/blob/977a9923fd551491623b6bfd398d5859488fe1ae/proposals/0000-derived-collection-of-enum-cases.md#possible-variations" style="color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1;background-color:transparent" target="_blank"><span style="font-weight:normal;font-size:16px;line-height:1;font-family:octicons;display:inline-block;vertical-align:middle"></span></a>Possible variations</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px">I&#39;d like us to discuss these, but they should be folded into either <em>Proposed solution</em> or <em>Future directions</em> before the proposal is submitted for review.</p><ul style="padding:0px 0px 0px 2em;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px"><li style="margin-left:15px"><p style="margin-top:16px;margin-bottom:16px">For enums with raw values, a static <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">rawValues</code> property (a collection of <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">RawValue</code> rather than the enum type itself) could also be synthesized.</p></li><li style="margin-left:15px"><p style="margin-top:16px;margin-bottom:16px">CaseEnumerable could have a user-visible declaration requiring <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">static var cases</code>, which would allow users to add conformances for custom non-<code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">enum</code> types.</p><ul style="padding:0px 0px 0px 2em;margin-top:0px;margin-bottom:0px"><li style="margin-left:15px">In this case, adding a conformance for a non-<code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">enum</code> type would not be a compiler error, it would just require an explicit implementation of <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">static var cases</code>, since the compiler wouldn&#39;t synthesize it.</li><li style="margin-left:15px">This would probably require <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">cases</code> to be <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">AnySequence&lt;Self&gt;</code>, or to introduce an <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">AnyCollection</code>, since we aren&#39;t able to say <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">associatedtype CaseCollection: CollectionType where CaseCollection.Generator.Element == Self</code>.</li></ul></li><li style="margin-left:15px"><p style="margin-top:16px;margin-bottom:16px">It would be nice to have a way of supporting this for OptionSetType structs. I would recommend that <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">cases</code> for an OptionSetType should include only the already-declared static properties (not all possible combinations of them). However, I&#39;m not sure it fits into this proposal.</p></li></ul><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;"><a href="https://github.com/jtbandes/swift-evolution/blob/977a9923fd551491623b6bfd398d5859488fe1ae/proposals/0000-derived-collection-of-enum-cases.md#impact-on-existing-code" style="color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1;background-color:transparent" target="_blank"><span style="font-weight:normal;font-size:16px;line-height:1;font-family:octicons;display:inline-block;vertical-align:middle"></span></a>Impact on existing code</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px">This proposal only adds functionality, so existing code will not be affected. (The identifier <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">CaseEnumerable</code> doesn&#39;t make any significant appearances in Google and GitHub searches.)</p><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;"><a href="https://github.com/jtbandes/swift-evolution/blob/977a9923fd551491623b6bfd398d5859488fe1ae/proposals/0000-derived-collection-of-enum-cases.md#alternatives-considered" style="color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1;background-color:transparent" target="_blank"><span style="font-weight:normal;font-size:16px;line-height:1;font-family:octicons;display:inline-block;vertical-align:middle"></span></a>Alternatives considered</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px">The community has not raised any solutions that differ significantly from this proposal, except for solutions which provide strictly <strong>more</strong> functionality. These are covered in the next section, <em>Future directions</em>.</p><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px">An alternative is to <em>not</em> implement this feature. The cons of this are discussed in the <em>Motivation</em> section above.</p><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px">The functionality could also be provided entirely through the Mirror/reflection APIs, but this would result in much more obscure and confusing usage patterns.</p><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;"><a href="https://github.com/jtbandes/swift-evolution/blob/977a9923fd551491623b6bfd398d5859488fe1ae/proposals/0000-derived-collection-of-enum-cases.md#future-directions" style="color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1;background-color:transparent" target="_blank"><span style="font-weight:normal;font-size:16px;line-height:1;font-family:octicons;display:inline-block;vertical-align:middle"></span></a>Future directions</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px">Many people would be happy to see even more functionality than what&#39;s proposed here. I&#39;m keeping this proposal intentionally limited, but I hope the community can continue discussing the topic to flesh out more features.</p><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px">Here are some starting points, which are <strong>not</strong> part of this proposal:</p><ul style="padding:0px 0px 0px 2em;margin-top:0px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;line-height:25.6px;margin-bottom:0px!important"><li style="margin-left:15px"><p style="margin-top:16px;margin-bottom:16px">Support for enum case <strong>names</strong>. It would be useful to get case names even for enums which have integer rawValues. This could be part of the existing reflection APIs, or it could take the form of derived implementations of StringLiteralConvertible/CustomStringConvertible.</p></li><li style="margin-left:15px"><p style="margin-top:16px;margin-bottom:16px">Support for enums with associated values.</p><ul style="padding:0px 0px 0px 2em;margin-top:0px;margin-bottom:0px"><li style="margin-left:15px"><p style="margin-top:16px;margin-bottom:16px">When all associated values are themselves CaseEnumerable, this could happen automatically:</p><div style="margin-bottom:16px"><pre style="white-space:pre-wrap;overflow:auto;font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;font-stretch:normal;line-height:1.45;padding:16px;border-radius:3px;word-wrap:normal;background-color:rgb(247,247,247)"><span style="color:rgb(167,29,93)">enum</span> Suit: CaseEnumerable { <span style="color:rgb(167,29,93)">case</span> Spades, Hearts, Diamonds, Clubs }
<span style="color:rgb(167,29,93)">enum</span> Rank: <span style="color:rgb(0,134,179)">Int</span>, CaseEnumerable {
    <span style="color:rgb(167,29,93)">case</span> Ace <span style="color:rgb(167,29,93)">=</span> <span style="color:rgb(0,134,179)">1</span>, Two, Three, Four, Five, Six
    <span style="color:rgb(167,29,93)">case</span> Seven, Eight, Nine, Ten, Jack, Queen, King
}
<span style="color:rgb(167,29,93)">enum</span> Card {
    <span style="color:rgb(167,29,93)">case</span> Joker
    <span style="color:rgb(167,29,93)">case</span> Value(Rank, Suit)
}

<span style="color:rgb(150,152,150)">// This now works, and generates all possible card types (Joker, Value(Ace, Spades), ...)</span>
<span style="color:rgb(167,29,93)">extension</span> Card: CaseEnumerable {}</pre></div></li><li style="margin-left:15px"><p style="margin-top:16px;margin-bottom:16px">If associated values aren&#39;t CaseEnumerable, but all cases are homogeneous, the <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">cases</code> collection could vend functions of <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">AssociatedValueType -&gt; EnumType</code>:</p><div style="margin-bottom:16px"><pre style="white-space:pre-wrap;overflow:auto;font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;font-stretch:normal;line-height:1.45;padding:16px;border-radius:3px;word-wrap:normal;background-color:rgb(247,247,247)"><span style="color:rgb(167,29,93)">enum</span> LogMessage { <span style="color:rgb(167,29,93)">case</span> Error(<span style="color:rgb(0,134,179)">String</span>), Warning(<span style="color:rgb(0,134,179)">String</span>), Info(<span style="color:rgb(0,134,179)">String</span>) }
<span style="color:rgb(167,29,93)">extension</span> LogMessage: CaseEnumerable {}

LogMessage<span style="color:rgb(167,29,93)">.</span>cases  <span style="color:rgb(150,152,150)">// elements are (String) -&gt; LogMessage</span></pre></div></li><li style="margin-left:15px"><p style="margin-top:16px;margin-bottom:16px">If Swift had anonymous sum types like <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">A | B | C</code>, then <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">E.cases</code> could vend elements of type <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">A-&gt;E | B-&gt;E | C-&gt;E</code>.</p><div style="margin-bottom:16px"><pre style="white-space:pre-wrap;overflow:auto;font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;font-stretch:normal;line-height:1.45;padding:16px;border-radius:3px;word-wrap:normal;background-color:rgb(247,247,247)"><span style="color:rgb(167,29,93)">enum</span> Expr { <span style="color:rgb(167,29,93)">case</span> Apply(Expr, Expr), Tuple(Expr, Expr), Literal(<span style="color:rgb(0,134,179)">Int</span>) }
<span style="color:rgb(167,29,93)">extension</span> Value: CaseEnumerable {}

<span style="color:rgb(150,152,150)">// This example is pretty contrived, but illustrates the functionality.</span>
<span style="color:rgb(167,29,93)">let</span> fortyTwos <span style="color:rgb(167,29,93)">=</span> Expr<span style="color:rgb(167,29,93)">.</span>cases<span style="color:rgb(167,29,93)">.</span><span style="color:rgb(0,134,179)">map</span> {
   <span style="color:rgb(150,152,150)">// $0 is of type `Int -&gt; Expr | (Expr, Expr) -&gt; Expr`</span>
   <span style="color:rgb(167,29,93)">switch</span> $<span style="color:rgb(0,134,179)">0</span> {
   <span style="color:rgb(167,29,93)">case</span> <span style="color:rgb(167,29,93)">let</span> lit <span style="color:rgb(167,29,93)">as</span> <span style="color:rgb(0,134,179)">Int</span> <span style="color:rgb(167,29,93)">-&gt;</span> Expr:  <span style="color:rgb(150,152,150)">// handles .Literal</span>
       <span style="color:rgb(167,29,93)">return</span> lit(<span style="color:rgb(0,134,179)">42</span>)
   <span style="color:rgb(167,29,93)">case</span> <span style="color:rgb(167,29,93)">let</span> bin <span style="color:rgb(167,29,93)">as</span> (Expr, Expr) <span style="color:rgb(167,29,93)">-&gt;</span> Expr:  <span style="color:rgb(150,152,150)">// handles .Apply and .Tuple</span>
       <span style="color:rgb(167,29,93)">return</span> bin(<span style="color:rgb(167,29,93)">.</span>Literal(<span style="color:rgb(0,134,179)">42</span>), <span style="color:rgb(167,29,93)">.</span>Literal(<span style="color:rgb(0,134,179)">42</span>))
   <span style="color:rgb(150,152,150)">// all cases are covered</span>
   }
}</pre></div></li></ul></li><li style="margin-left:15px"><p style="margin-top:16px;margin-bottom:16px">Support for generic enums.</p><ul style="padding:0px 0px 0px 2em;margin-top:0px;margin-bottom:0px"><li style="margin-left:15px"><p style="margin-top:16px;margin-bottom:16px">CaseEnumerable could be conditionally supported depending on the generic argument(s). A great example would be Optional:</p><div style="margin-bottom:16px"><pre style="white-space:pre-wrap;overflow:auto;font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;font-stretch:normal;line-height:1.45;padding:16px;border-radius:3px;word-wrap:normal;background-color:rgb(247,247,247)"><span style="color:rgb(167,29,93)">enum</span> MyEnum: CaseEnumerable {}
<span style="color:rgb(167,29,93)">extension</span> <span style="color:rgb(0,134,179)">Optional</span>: CaseEnumerable <span style="color:rgb(167,29,93)">where</span> Wrapped: CaseEnumerable {}

<span style="color:rgb(150,152,150)">// Optional&lt;MyEnum&gt;.cases effectively contains `MyEnum.cases + [.None]`</span></pre></div></li></ul></li></ul></div></div></div></div></div></div></div>
</div></div></div>
_______________________________________________<span class=""><br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></span></div></blockquote></div><br></div></div></blockquote></div><br></div></div>