Last active
November 5, 2016 06:12
-
-
Save wh1pch81n/7db84de9dbb76f25253c9e8987a838dc to your computer and use it in GitHub Desktop.
A method to help count enums. The switch statement will enforce you cover a state where you count. This is in response to this: https://github.com/jtbandes/swift-evolution/blob/case-enumerable/proposals/0000-derived-collection-of-enum-cases.md
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
enum Planets { | |
case Mercury | |
case Venus | |
case Earth | |
case Mars | |
case Jupiter | |
static let count: Int = _count() | |
private static func _count(_ first: Planets = .Mercury) -> Int { | |
print("blahp") | |
var runningTotal = 0 | |
switch first { | |
case .Mercury: runningTotal += 1; fallthrough | |
case .Venus: runningTotal += 1; fallthrough | |
case .Earth: runningTotal += 1; fallthrough | |
case .Mars: runningTotal += 1; fallthrough | |
case .Jupiter: runningTotal += 1; //fallthrough | |
} | |
return runningTotal | |
} | |
static let count2: Int = _count2() | |
private static func _count2(_ first: Planets = .Mercury) -> Int { | |
print("pppp") | |
switch first { | |
case .Mercury: return 1 + _count2(.Venus) | |
case .Venus: return 1 + _count2(.Earth) | |
case .Earth: return 1 + _count2(.Mars) | |
case .Mars: return 1 + _count2(.Jupiter) | |
case .Jupiter: return 1 // + count2( | |
} | |
} | |
} | |
Planets.count // 5 | |
Planets.count2 // 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment