<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>I would like to see in this proposal go into review.<br><br></div><div><br>On Mar 9, 2016, at 12:45 AM, Jacob Bandes-Storch via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr">Hi Jose,<div>Enums with payloads were discussed, but the proposal considers them out of scope — I was trying to keep it minimal so we could get a first version into the language. (Still waiting for it to be merged though...! <a href="https://github.com/apple/swift-evolution/pull/114">https://github.com/apple/swift-evolution/pull/114</a>&nbsp;)</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 Tue, Mar 8, 2016 at 6:34 PM, Jose Cheyo Jimenez <span dir="ltr">&lt;<a href="mailto:cheyo@masters3d.com" target="_blank">cheyo@masters3d.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word"><div>Hi Jacob,&nbsp;</div><div><br></div><div>This may be totally unrelated but how would proposed protocol handle enums with payloads?</div><div>I field a SR requesting a way to get the declared order cases of enums with payloads.&nbsp;</div><div><a href="https://bugs.swift.org/browse/SR-887" target="_blank">https://bugs.swift.org/browse/SR-887</a></div><div><br></div><div>Do you think your proposal could be expanded to include Enums with payloads?&nbsp;</div><div><br></div><div>Thanks!</div><span class=""><div><br></div>
&gt; Hi folks,<br>&gt; <br>&gt; I've drafted a proposal to add a CaseEnumerable protocol, which will derive<br>&gt; a static variable "cases" for enum types. Feedback is welcome, especially<br>&gt; for refining the proposal before I submit a formal PR.<br>&gt; <br>&gt; The draft is here; full text below.<br>&gt; <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><br>&gt; <br>&gt; <br>&gt; Derived Collection of Enum Cases<br>&gt; <br></span>&gt; - Proposal: SE-NNNN<br>&gt; &lt;<a href="https://github.com/apple/swift-evolution/blob/master/proposals/NNNN-derived-collection-of-enum-cases.md" target="_blank">https://github.com/apple/swift-evolution/blob/master/proposals/NNNN-derived-collection-of-enum-cases.md</a>&gt;<br>&gt; - Author(s): Jacob Bandes-Storch&lt;<a href="https://github.com/jtbandes" target="_blank">https://github.com/jtbandes</a>&gt;<br>&gt; - Status: *Awaiting review*<br>&gt; - Review manager: TBD<br>&gt; <br>&gt; &lt;<a href="https://github.com/jtbandes/swift-evolution/blob/977a9923fd551491623b6bfd398d5859488fe1ae/proposals/0000-derived-collection-of-enum-cases.md#introduction" target="_blank">https://github.com/jtbandes/swift-evolution/blob/977a9923fd551491623b6bfd398d5859488fe1ae/proposals/0000-derived-collection-of-enum-cases.md#introduction</a>&gt;<span class=""><br>&gt; Introduction<br>&gt; <br>&gt; It is a truth universally acknowledged, that a programmer in possession of<br>&gt; an enum with many cases, must eventually be in want of dynamic enumeration<br>&gt; over them.<br>&gt; <br>&gt; This topic has come up three times on the swift-evolution mailing list so<br>&gt; far:<br>&gt; <br></span>&gt; - List of all Enum values (for simple enums)<br>&gt; &lt;<a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/001233.html" target="_blank">https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/001233.html</a>&gt;<br>&gt; (December<br>&gt; 8, 2015)<br>&gt; - Proposal: Enum 'count' functionality<br>&gt; &lt;<a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151221/003819.html" target="_blank">https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151221/003819.html</a>&gt;<br>&gt; (December<br>&gt; 21, 2015)<br>&gt; - Draft Proposal: count property for enum types<br>&gt; &lt;<a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160111/006853.html" target="_blank">https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160111/006853.html</a>&gt;<span class=""><br>&gt; (January<br>&gt; 17, 2016)<br>&gt; <br>&gt; Enumerating enumerations in Swift is also a popular topic on Stack Overflow:<br>&gt; <br></span>&gt; - How to enumerate an enum with String type?<br>&gt; &lt;<a href="http://stackoverflow.com/questions/24007461/how-to-enumerate-an-enum-with-string-type" target="_blank">http://stackoverflow.com/questions/24007461/how-to-enumerate-an-enum-with-string-type</a>&gt;<span class=""><br>&gt; (June<br>&gt; 3, 2014; question score 131)<br></span>&gt; - How do I get the count of a Swift enum?<br>&gt; &lt;<a href="http://stackoverflow.com/questions/27094878/how-do-i-get-the-count-of-a-swift-enum" target="_blank">http://stackoverflow.com/questions/27094878/how-do-i-get-the-count-of-a-swift-enum</a>&gt;<span class=""><br>&gt; (November<br>&gt; 23, 2014; question score 37)<br>&gt; <br></span>&gt; &lt;<a href="https://github.com/jtbandes/swift-evolution/blob/977a9923fd551491623b6bfd398d5859488fe1ae/proposals/0000-derived-collection-of-enum-cases.md#motivation" target="_blank">https://github.com/jtbandes/swift-evolution/blob/977a9923fd551491623b6bfd398d5859488fe1ae/proposals/0000-derived-collection-of-enum-cases.md#motivation</a>&gt;<span class=""><br>&gt; Motivation<br>&gt; <br>&gt; Simple enums are finite, and their values are statically known to the<br>&gt; compiler, yet working with them programmatically is challenging. It is<br>&gt; often desirable to iterate over all possible cases of an enum, or to know<br>&gt; the number of cases (or maximum valid rawValue).<br>&gt; <br>&gt; Currently, however, there is no built-in reflection or enumeration support.<br>&gt; Users must resort to manually listing out cases in order to iterate over<br>&gt; them:<br>&gt; <br>&gt; enum Attribute {<br>&gt; case Date, Name, Author<br>&gt; }func valueForAttribute(attr: Attribute) -&gt;String { …from elsewhere… }<br>&gt; // Cases must be listed explicitly:<br>&gt; [Attribute.Date, .Name, .Author].map{ valueForAttribute($0)<br>&gt; }.joinWithSeparator("\n")<br>&gt; <br>&gt; For RawRepresentable enums, users have often relied on iterating over the<br>&gt; known (or assumed) allowable raw values:<br>&gt; <br></span>&gt; *Annotated excerpt from Nate Cook's post, Loopy, Random Ideas for Extending<br>&gt; "enum"&lt;<a href="http://natecook.com/blog/2014/10/loopy-random-enum-ideas/" target="_blank">http://natecook.com/blog/2014/10/loopy-random-enum-ideas/</a>&gt;(October<br>&gt; 2014):*<span class=""><br>&gt; <br>&gt; enum Reindeer: Int {<br>&gt; case Dasher, Dancer, Prancer, Vixen, Comet, Cupid, Donner,<br>&gt; Blitzen, Rudolph<br>&gt; }extension Reindeer {<br>&gt; static var allCases: [Reindeer] {<br>&gt; var cur = 0<br>&gt; return Array(<br>&gt; GeneratorOf&lt;Reindeer&gt;{<br>&gt; return Reindeer(rawValue: cur++)<br>&gt; }<br>&gt; )<br>&gt; }<br>&gt; static var caseCount: Int {<br>&gt; var max: Int = 0<br>&gt; while let _ = self(rawValue: ++max) {}<br>&gt; return max<br>&gt; }<br>&gt; static func randomCase() -&gt;Reindeer {<br>&gt; // everybody do the Int/UInt32 shuffle!<br>&gt; let randomValue = Int(arc4random_uniform(UInt32(caseCount)))<br>&gt; return self(rawValue: randomValue)!<br>&gt; }<br>&gt; }<br>&gt; <br>&gt; There are many problems with these existing techniques:<br>&gt; <br></span>&gt; - They are ad-hoc and can't benefit every enum type without duplicated<br>&gt; and code.<br>&gt; - They are not standardized across codebases, nor provided automatically<span class=""><br>&gt; by libraries such as Foundation and {App,UI}Kit.<br></span>&gt; - They are sometimes prone to bugs when enum cases are added, but the<span class=""><br>&gt; user forgets to update a hard-coded static collection of cases.<br>&gt; <br></span>&gt; &lt;<a href="https://github.com/jtbandes/swift-evolution/blob/977a9923fd551491623b6bfd398d5859488fe1ae/proposals/0000-derived-collection-of-enum-cases.md#precedent-in-other-languages" target="_blank">https://github.com/jtbandes/swift-evolution/blob/977a9923fd551491623b6bfd398d5859488fe1ae/proposals/0000-derived-collection-of-enum-cases.md#precedent-in-other-languages</a>&gt;Precedent<br>&gt; in other languages<br>&gt; <br>&gt; -<span class=""><br>&gt; <br>&gt; Rust does not seem to have a solution for this problem.<br></span>&gt; -<span class=""><br>&gt; <br>&gt; C#'s Enum has several methods<br></span>&gt; &lt;<a href="https://msdn.microsoft.com/en-us/library/system.enum_methods.aspx" target="_blank">https://msdn.microsoft.com/en-us/library/system.enum_methods.aspx</a>&gt;<span class=""><br>&gt; available<br>&gt; for reflection, including GetValues() and GetNames().<br></span>&gt; -<br>&gt; <br>&gt; Java implicitly declares<br>&gt; &lt;<a href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.9.3" target="_blank">http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.9.3</a>&gt;a<span class=""><br>&gt; static values() function, returning an array of enum values, and<br>&gt; valueOf(String<br>&gt; name) which takes a String and returns the enum value with the<br>&gt; corresponding name (or throws an exception). More examples here<br></span>&gt; &lt;<a href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.9.3" target="_blank">http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.9.3</a>&gt;.<br>&gt; -<span class=""><br>&gt; <br>&gt; The Template Haskell extension to Haskell provides a function reify which<br>&gt; extracts info about types<br></span>&gt; &lt;<a href="http://hackage.haskell.org/package/template-haskell-2.10.0.0/docs/Language-Haskell-TH-Syntax.html#t:Info" target="_blank">http://hackage.haskell.org/package/template-haskell-2.10.0.0/docs/Language-Haskell-TH-Syntax.html#t:Info</a>&gt;,<br>&gt; including their constructors.<br>&gt; <br>&gt; &lt;<a href="https://github.com/jtbandes/swift-evolution/blob/977a9923fd551491623b6bfd398d5859488fe1ae/proposals/0000-derived-collection-of-enum-cases.md#proposed-solution" target="_blank">https://github.com/jtbandes/swift-evolution/blob/977a9923fd551491623b6bfd398d5859488fe1ae/proposals/0000-derived-collection-of-enum-cases.md#proposed-solution</a>&gt;Proposed<span class=""><br>&gt; solution<br>&gt; <br>&gt; Introduce a CaseEnumerable protocol. Conforming to CaseEnumerable will<br>&gt; automagically derive a static var cases, whose type is a CollectionType of<br>&gt; all the enum's values.<br>&gt; <br>&gt; Like ErrorType, the CaseEnumerable protocol will not have any user-visible<br>&gt; requirements; merely adding the conformance is enough to enable case<br>&gt; enumeration.<br>&gt; <br>&gt; enum Ma { case 马, 吗, 妈, 码, 骂, 🐎, 🐴 }<br>&gt; extension Ma: CaseEnumerable {}<br>&gt; <br>&gt; Ma.cases // returns some CollectionType whose Generator.Element is Ma<br></span>&gt; Ma.cases.count // returns 7Array(Ma.cases) // returns [Ma.马, .吗,<span class=""><br>&gt; .妈, .码, .骂, .🐎, .🐴]<br>&gt; <br>&gt; Conformances can even be added for enums which are defined in other modules:<br>&gt; <br>&gt; extension NSTextAlignment: CaseEnumerable {}<br>&gt; Array(NSTextAlignment.cases) // returns [NSTextAlignment.Left,<br>&gt; .Right, .Center, .Justified, .Natural]<br>&gt; <br></span>&gt; &lt;<a href="https://github.com/jtbandes/swift-evolution/blob/977a9923fd551491623b6bfd398d5859488fe1ae/proposals/0000-derived-collection-of-enum-cases.md#detailed-design" target="_blank">https://github.com/jtbandes/swift-evolution/blob/977a9923fd551491623b6bfd398d5859488fe1ae/proposals/0000-derived-collection-of-enum-cases.md#detailed-design</a>&gt;Detailed<span class=""><br>&gt; design<br>&gt; <br>&gt; Enum cases are enumerated in the order they appear in the source code.<br>&gt; <br>&gt; The cases collection does not necessitate Ω(number of cases) static<br>&gt; storage. For integer-backed enums, only the range(s) of valid rawValues<br>&gt; need to be stored, and the enum construction can happen dynamically.<br>&gt; <br>&gt; Attempting to derive CaseEnumerable for a non-enum type will result in a<br>&gt; compiler error.<br>&gt; <br>&gt; Attempting to derive CaseEnumerable for an enum with associated values will<br>&gt; result in a compiler error.<br></span>&gt; &lt;<a href="https://github.com/jtbandes/swift-evolution/blob/977a9923fd551491623b6bfd398d5859488fe1ae/proposals/0000-derived-collection-of-enum-cases.md#possible-variations" target="_blank">https://github.com/jtbandes/swift-evolution/blob/977a9923fd551491623b6bfd398d5859488fe1ae/proposals/0000-derived-collection-of-enum-cases.md#possible-variations</a>&gt;Possible<br>&gt; variations<br>&gt; <br>&gt; I'd like us to discuss these, but they should be folded into either *Proposed<br>&gt; solution* or *Future directions* before the proposal is submitted for<br>&gt; review.<br>&gt; <br>&gt; -<span class=""><br>&gt; <br>&gt; For enums with raw values, a static rawValues property (a collection of<br>&gt; RawValue rather than the enum type itself) could also be synthesized.<br></span>&gt; -<span class=""><br>&gt; <br>&gt; CaseEnumerable could have a user-visible declaration requiring static<br>&gt; var cases, which would allow users to add conformances for custom non-<br>&gt; enum types.<br></span>&gt; - In this case, adding a conformance for a non-enum type would not be a<span class=""><br>&gt; compiler error, it would just require an explicit implementation<br>&gt; of static<br>&gt; var cases, since the compiler wouldn't synthesize it.<br></span>&gt; - This would probably require cases to be AnySequence&lt;Self&gt;, or to<span class=""><br>&gt; introduce an AnyCollection, since we aren't able to say associatedtype<br>&gt; CaseCollection: CollectionType where CaseCollection.Generator.Element ==<br>&gt; Self.<br></span>&gt; -<span class=""><br>&gt; <br>&gt; It would be nice to have a way of supporting this for OptionSetType<br>&gt; structs. I would recommend that cases for an OptionSetType should<br>&gt; include only the already-declared static properties (not all possible<br>&gt; combinations of them). However, I'm not sure it fits into this proposal.<br>&gt; <br></span>&gt; &lt;<a href="https://github.com/jtbandes/swift-evolution/blob/977a9923fd551491623b6bfd398d5859488fe1ae/proposals/0000-derived-collection-of-enum-cases.md#impact-on-existing-code" target="_blank">https://github.com/jtbandes/swift-evolution/blob/977a9923fd551491623b6bfd398d5859488fe1ae/proposals/0000-derived-collection-of-enum-cases.md#impact-on-existing-code</a>&gt;Impact<span class=""><br>&gt; on existing code<br>&gt; <br>&gt; This proposal only adds functionality, so existing code will not be<br>&gt; affected. (The identifier CaseEnumerable doesn't make any significant<br>&gt; appearances in Google and GitHub searches.)<br></span>&gt; &lt;<a href="https://github.com/jtbandes/swift-evolution/blob/977a9923fd551491623b6bfd398d5859488fe1ae/proposals/0000-derived-collection-of-enum-cases.md#alternatives-considered" target="_blank">https://github.com/jtbandes/swift-evolution/blob/977a9923fd551491623b6bfd398d5859488fe1ae/proposals/0000-derived-collection-of-enum-cases.md#alternatives-considered</a>&gt;Alternatives<span class=""><br>&gt; considered<br>&gt; <br>&gt; The community has not raised any solutions that differ significantly from<br></span>&gt; this proposal, except for solutions which provide strictly *more*<br>&gt; functionality.<br>&gt; These are covered in the next section, *Future directions*.<br>&gt; <br>&gt; An alternative is to *not* implement this feature. The cons of this are<br>&gt; discussed in the *Motivation* section above.<span class=""><br>&gt; <br>&gt; The functionality could also be provided entirely through the<br>&gt; Mirror/reflection APIs, but this would result in much more obscure and<br>&gt; confusing usage patterns.<br></span>&gt; &lt;<a href="https://github.com/jtbandes/swift-evolution/blob/977a9923fd551491623b6bfd398d5859488fe1ae/proposals/0000-derived-collection-of-enum-cases.md#future-directions" target="_blank">https://github.com/jtbandes/swift-evolution/blob/977a9923fd551491623b6bfd398d5859488fe1ae/proposals/0000-derived-collection-of-enum-cases.md#future-directions</a>&gt;Future<span class=""><br>&gt; directions<br>&gt; <br>&gt; Many people would be happy to see even more functionality than what's<br>&gt; proposed here. I'm keeping this proposal intentionally limited, but I hope<br>&gt; the community can continue discussing the topic to flesh out more features.<br>&gt; <br></span>&gt; Here are some starting points, which are *not* part of this proposal:<br>&gt; <br>&gt; -<br>&gt; <br>&gt; Support for enum case *names*. It would be useful to get case names even<span class=""><br>&gt; for enums which have integer rawValues. This could be part of the existing<br>&gt; reflection APIs, or it could take the form of derived implementations of<br>&gt; StringLiteralConvertible/CustomStringConvertible.<br></span>&gt; -<span class=""><br>&gt; <br>&gt; Support for enums with associated values.<br></span>&gt; -<span class=""><br>&gt; <br>&gt; When all associated values are themselves CaseEnumerable, this could<br>&gt; happen automatically:<br>&gt; <br>&gt; enum Suit: CaseEnumerable { case Spades, Hearts, Diamonds, Clubs<br>&gt; }enum Rank: Int, CaseEnumerable {<br>&gt; case Ace = 1, Two, Three, Four, Five, Six<br>&gt; case Seven, Eight, Nine, Ten, Jack, Queen, King<br>&gt; }enum Card {<br>&gt; case Joker<br>&gt; case Value(Rank, Suit)<br>&gt; }<br>&gt; // This now works, and generates all possible card types (Joker,<br>&gt; Value(Ace, Spades), ...)extension Card: CaseEnumerable {}<br>&gt; <br></span>&gt; -<span class=""><br>&gt; <br>&gt; If associated values aren't CaseEnumerable, but all cases are<br>&gt; homogeneous, the cases collection could vend functions of<br>&gt; AssociatedValueType<br>&gt; -&gt;EnumType:<br>&gt; <br>&gt; enum LogMessage { case Error(String), Warning(String),<br>&gt; Info(String) }extension LogMessage: CaseEnumerable {}<br>&gt; <br>&gt; LogMessage.cases // elements are (String) -&gt;LogMessage<br>&gt; <br></span>&gt; -<span class=""><br>&gt; <br>&gt; If Swift had anonymous sum types like A | B | C, then E.cases could<br>&gt; vend elements of type A-&gt;E | B-&gt;E | C-&gt;E.<br>&gt; <br>&gt; enum Expr { case Apply(Expr, Expr), Tuple(Expr, Expr),<br>&gt; Literal(Int) }extension Value: CaseEnumerable {}<br>&gt; // This example is pretty contrived, but illustrates the<br></span>&gt; functionality.let fortyTwos = Expr.cases.map {<span class=""><br>&gt; // $0 is of type `Int -&gt;Expr | (Expr, Expr) -&gt;Expr`<br>&gt; switch $0 {<br>&gt; case let lit as Int -&gt;Expr: // handles .Literal<br>&gt; return lit(42)<br>&gt; case let bin as (Expr, Expr) -&gt;Expr: // handles .Apply and .Tuple<br>&gt; return bin(.Literal(42), .Literal(42))<br>&gt; // all cases are covered<br>&gt; }<br>&gt; }<br>&gt; <br></span>&gt; -<br>&gt; <br>&gt; Support for generic enums.<br>&gt; -<span class=""><br>&gt; <br>&gt; CaseEnumerable could be conditionally supported depending on the<br>&gt; generic argument(s). A great example would be Optional:<br>&gt; <br>&gt; enum MyEnum: CaseEnumerable {}extension Optional: CaseEnumerable<br>&gt; where Wrapped: CaseEnumerable {}<br>&gt; // Optional&lt;MyEnum&gt;.cases effectively contains `MyEnum.cases + [.None]`<br>&gt; <br>&gt; <br>&gt;<span>&nbsp;</span>

</span></div></blockquote></div><br></div></div>
</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>