[swift-evolution] Rough Proposal - Collection of all cases for an Enum

Anthony Miller anthonymdev at gmail.com
Wed Dec 23 16:41:13 CST 2015


Rough proposal for implementing a method to get all cases on a enum. I feel like this is a valuable feature, and all current methods of doing this have major drawbacks. I'd like to field the idea to gauge interest.
Thank You,
Anthony Miller
Lead iOS Developer, App-Order.com <http://app-order.com/>
Mobile: 702-334-8454
# Collection of all cases for an Enum

## Introduction
Add reflection capabilities for getting an `Array` or `Set` of all cases for an Enum. 
This would not be supported for enums with associated values.
## Motivation
This feature provides a benefit for many use cases. 
Multiple methods of doing this exist currently, however each has obvious drawbacks.
For an summary of current methods, see: http://stackoverflow.com/questions/24007461/how-to-enumerate-an-enum-with-string-type/24137319#24137319 <http://stackoverflow.com/questions/24007461/how-to-enumerate-an-enum-with-string-type/24137319#24137319>
## Proposed solution

This could be implemented as a static function or static constant. 
    enum Foo {
        case CaseOne, CaseTwo, CaseThree
    }
    
    let allCases: [Foo] = Foo.allCases()    // [.CaseOne, .CaseTwo, .CaseThree]    

Alternatively, an extension of `Mirror` to support mirrors of types could include this capability.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151223/986e11ee/attachment.html>


More information about the swift-evolution mailing list