For demo purposes I've created next code:
enum WeatherType {
case cloudy(coverage: Int)
case sunny
case rainy
}
let today: WeatherType = .cloudy(coverage: 0)
switch today {
case .cloudy(let coverage) where coverage == 0, .sunny: // <-- This line doesn't compile
print("☀️")
case .cloudy(let coverage) where 1...100 ~= coverage:
print("☁️")
case .rainy:
print("")
default:
print("Unknown weather")
}
The compile error message is 'coverage' must be bound in every pattern. As I've already googled that a way to go with associated values is to compare different states of values within the same enum case. But this may lead to code duplication, like in my example, where I need to write two case statements for .sunny and .cloudy(let coverage) where coverage == 0.
Is there any correct, swifty way, to deal with such cases?