[swift-evolution] Draft Proposal: count property for enum types

Martin Crossley martin at crossleys.biz
Sun Jan 17 05:39:22 CST 2016

I’m going to apologise if this has already been proposed a million times before, but I can’t find a way to search the list. I’m a novice here guys :)

I propose adding a read-only ‘count’ property to enum types, that returns the number of cases; modelled on the API for arrays types.

This has been discussed previously in user communities (e.g. see http://stackoverflow.com/questions/27094878/how-do-i-get-the-count-of-a-swift-enum <http://stackoverflow.com/questions/27094878/how-do-i-get-the-count-of-a-swift-enum>) but it seems to me the proposed solutions are a little complex and inelegant.

To answer the question “what problem does this solve?”, I’m thinking of a situation like this - granted, this is a bit artificial, but imagine if there were a very large number of states…)

enum Tristate: Int {
    case High = 0
    case Low
    case Off
    mutating func next() {
        let nextState = (self.rawValue + 1) % Tristate.count
        self = Tristate(rawValue: nextState)!

thanks, Martin

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

More information about the swift-evolution mailing list