[swift-evolution] [Idea] `enum` case count feature

Víctor Pimentel Rodríguez vpimentel at tuenti.com
Thu May 11 05:55:10 CDT 2017

On Wed, May 10, 2017 at 8:52 PM, Saagar Jha via swift-evolution <
swift-evolution at 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.

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.

Víctor Pimentel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170511/1033ff86/attachment.html>

More information about the swift-evolution mailing list