Swift 2 introduced if-case
which is supposed to be a more terse way of expressing a switch that only has a few cases. I'm wondering if there is a way of expressing a switch statement with a comma delimited list as an if-case
.
let (a,b) = (1,0)
switch (a,b) {
case (1,0), (0,1), (1,1):
print("true")
default:
print("false")
}
What I've tried:
if case (a,b) = (1,0) {
// works
}
if case (a,b) = (1,0), (0,1), (1,1) {
// doesn't
}
This compiles but returns false for the (a,b)
tuple:
if case (a,b) = (1,0),
case (a,b) = (0,1),
case (a,b) = (1,1) {
print("if case true")
} else {
print("if case false")
}
What I'd like to see:
I'd like to see an approach to shorten the above switch statement into a single if-case
.
My Question
Is it possible to use if-case
in place of a switch that has a coma delimited list as a case?