Skip to content

Instantly share code, notes, and snippets.

@jakebromberg
Created November 15, 2017 23:34
Show Gist options
  • Save jakebromberg/14fae88997dd229a0282db4b405c7474 to your computer and use it in GitHub Desktop.
Save jakebromberg/14fae88997dd229a0282db4b405c7474 to your computer and use it in GitHub Desktop.
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