-
-
Save chapayGhub/db7e2b25930c26fd357112fd621091c9 to your computer and use it in GitHub Desktop.
count and allValues on Int-based enums
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| protocol CaseCountable { } | |
| extension CaseCountable where Self : RawRepresentable, Self.RawValue == Int { | |
| static var count: Int { | |
| var count = 0 | |
| while let _ = Self(rawValue: count) { count+=1 } | |
| return count | |
| } | |
| static var allValues: [Self] { | |
| return (0..<count).flatMap({ Self(rawValue: $0) }) | |
| } | |
| } | |
| enum Someenum: Int { | |
| case one, two, three, four | |
| } | |
| extension Someenum: CaseCountable { } | |
| Someenum.count | |
| Someenum.allValues |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment