3

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?

1 Answer 1

0

Well, after nearly a month. This is the closest I could get to satisfying the comma delimited list question. This actually satisfies the same requirement but in a different way.

let tuple = (1,0) if case (0...1, 0...1) = tuple where !(a == 0 && b == 0) { return true } else { return false } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.