Created
November 15, 2017 23:34
-
-
Save jakebromberg/14fae88997dd229a0282db4b405c7474 to your computer and use it in GitHub Desktop.
This file contains 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
extension RawRepresentable where RawValue: Numeric { | |
/// Returns all the values of a RawRepresentable whose RawValue is Numeric. This static property expressly exists | |
/// for enumerations, though can be used elsewhere. Works only for contiguous values. | |
/// Supposing we have: | |
/// ``` | |
/// enum Counter { | |
/// case one, two | |
/// } | |
/// ``` | |
/// then `Counter.allValues == [.one, .two]`. | |
static var allValues: [Self] { | |
var i: RawValue = 0 | |
return Array(AnyIterator { | |
let result = self.init(rawValue: i) | |
i += 1 | |
return result | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment