I'm using swift and I have encountered an error when using switch statement and greater than > to compare a number.
Xcode display the following message: expression pattern of type "Bool" cannot match values of type "Int"
I know that by replacing case self > 0: return .positive
with case let x where x > 0: return .positive
, everything works just fine.
But I don't really understand why case self > 0: return .positive
is not allowed? What is the reason behind it?
extension Int {
enum Kind {
case negative, zero, positive
}
var kind: Kind {
switch self {
case 0:
return .zero
//Error: expression pattern of type "Bool" cannot match values of type "Int"
// case self > 0:
// return .positive
case let x where x > 0: //this works
return .positive
default:
return .negative
}
}
}