Rough Proposal - Collection of all cases for an Enum


(Anthony Miller) #1

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
## 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.


(Félix Cloutier) #2

You should join your efforts to the "Enum 'count' functionality" <https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151221/003819.html> thread, where this is also being discussed.

Félix

···

Le 23 déc. 2015 à 17:41:13, Anthony Miller via swift-evolution <swift-evolution@swift.org> a écrit :

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
## 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.

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution