[Idea] `enum` case count feature


(Nicholas Maccharoli) #1

Swift-Evolution,

I'm sorry if this has been brought up before but is there a reason why
there is no built-in way of getting the number of cases an enum defines?

Given something like: enum MyEnum { case foo, bar, baz }

It would be nice to get the number of cases this enum defines, something
like:

    MyEnum.count

Looking forward to hearing back about this!

- Nick


(Brent Royal-Gordon) #2

We're way ahead of you:

* Discussion on this topic started in the early months of swift-evolution.

* We decided it'd be much more useful to instead have an `allCases` or `allValues` collection attached to the type.

* We've had a proposal for such a feature for the better part of a year.

* But the stars have never aligned to actually get it into the compiler, so it's never gotten past the initial proposal stage. (First it was waiting on new features that should land in Swift 4; then it didn't really fit with Swift 4's goals.)

Here's the most recent pull request for the proposal: <https://github.com/apple/swift-evolution/pull/114>

···

On May 10, 2017, at 12:58 AM, Nicholas Maccharoli via swift-evolution <swift-evolution@swift.org> wrote:

Swift-Evolution,

I'm sorry if this has been brought up before but is there a reason why there is no built-in way of getting the number of cases an enum defines?

Given something like: enum MyEnum { case foo, bar, baz }

It would be nice to get the number of cases this enum defines, something like:

    MyEnum.count

Looking forward to hearing back about this!

--
Brent Royal-Gordon
Architechies


(Yuya Hirayama) #3

Hi, Nick.
I have no idea about it and am interested in why you want to count the cases.

···

--
Yuya Hirayama

On 2017年5月10日 at 16:58:41, Nicholas Maccharoli via swift-evolution (swift-evolution@swift.org) wrote:

Swift-Evolution,

I'm sorry if this has been brought up before but is there a reason why there is no built-in way of getting the number of cases an enum defines?

Given something like: enum MyEnum { case foo, bar, baz }

It would be nice to get the number of cases this enum defines, something like:

MyEnum\.count

Looking forward to hearing back about this!

- Nick

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


(Saagar Jha) #4

Not Nicholas, but I thought I’d share one of my use cases.

I use a enum (with a Int raw value) for sections in a table view, so whenever the table view asks for the number of rows I pass in the number of cases. Currently, I’m doing something like this:

enum Sections: Int {
  case section1: 0
  case section2
  case section3
  case _count
}

Obviously, this cases issues in switch statements since I need to handle the _count case with an assertion, but other than that it works rather well.

Saagar Jha

···

On May 10, 2017, at 01:04, Yuya Hirayama via swift-evolution <swift-evolution@swift.org> wrote:

Hi, Nick.
I have no idea about it and am interested in why you want to count the cases.

--
Yuya Hirayama

On 2017年5月10日 at 16:58:41, Nicholas Maccharoli via swift-evolution (swift-evolution@swift.org <mailto:swift-evolution@swift.org>) wrote:

Swift-Evolution,

I'm sorry if this has been brought up before but is there a reason why there is no built-in way of getting the number of cases an enum defines?

Given something like: enum MyEnum { case foo, bar, baz }

It would be nice to get the number of cases this enum defines, something like:

    MyEnum.count

Looking forward to hearing back about this!

- Nick

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

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


(Víctor Pimentel) #5

Yes, it is useful for Table Views, and for pets projects I have done it, at
the expense of handling that case due to exhaustive switches. However as
Brent stated, it does not fit well into Swift 4 at this late stage, so
maybe if better introspection is introduced in Swift 5 it will fit better.

Just to be clear, if the proposal that Brent stated gets accepted you could
do something like this:

let count = Sections.allCases.count

And you won't have to put that assert never again.

···

On Wed, May 10, 2017 at 8:52 PM, Saagar Jha via swift-evolution < swift-evolution@swift.org> wrote:

I use a enum (with a Int raw value) for sections in a table view, so
whenever the table view asks for the number of rows I pass in the number of
cases. Currently, I’m doing something like this:

enum Sections: Int {
case section1: 0
case section2
case section3
case _count
}

Obviously, this cases issues in switch statements since I need to handle
the _count case with an assertion, but other than that it works rather well.

--
Víctor Pimentel