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

Saagar Jha saagar at saagarjha.com
Wed May 10 13:52:10 CDT 2017


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 at 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 at swift.org <mailto:swift-evolution at 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 at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170510/a8776373/attachment.html>


More information about the swift-evolution mailing list