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

</body></html>