Skip to main content
deleted 1 character in body
Source Link
Dan Beaulieu
  • 20k
  • 19
  • 107
  • 138

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 above variable(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?

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 above variable:

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?

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?

added 234 characters in body
Source Link
Dan Beaulieu
  • 20k
  • 19
  • 107
  • 138

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 above variable:

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?

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 } 

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?

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 above variable:

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?

added 2 characters in body
Source Link
Dan Beaulieu
  • 20k
  • 19
  • 107
  • 138

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 } 

What I'd like to see:

I'd like to see an approach to shorten the above switch statement into a single if-caseif-case.

My Question

Is it possible to use 'if-case'if-case in place of a switch that has a coma delimited list as a case?

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 } 

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?

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 } 

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?

added 2 characters in body
Source Link
Dan Beaulieu
  • 20k
  • 19
  • 107
  • 138
Loading
Source Link
Dan Beaulieu
  • 20k
  • 19
  • 107
  • 138
Loading