Switch / Case with multiple matches in same Case

Hello,

How does one match multiple possible Switch expression values in a single Case operation? Using OR (eg 2 || 3 ) results in that case being executed for all values (eg 2, 4, 9), while a comma isn’t accepted and trying =“2,3” doesn’t match either value.

Thanks,
James

At the moment you would have to use ‘if’ operations to achieve that.

E.g. assuming the value you were using in your switch statement was in a variable called ‘value’ then something like the following work for your example:

if ^value == 2 || value == 3^ {
	// do something
} 
if ^value == 4^ {
	// do something else
} 

In a classic switch statement to achieve that effect you would use blank cases without a break in them:

e.g.

switch value {
	case 2 {
	}
	case 3 {
		// do something
		break;
	}
	case 4 {
		// do something else
		break;
	} 
} 

That behavior is called fallthrough. That behavior isn’t the default because it would require a lot of break statements adding clutter and most use cases don’t need it. It’s widely considered it was a mistake that fallthrough was the default in many standard languages. There’s a good discussion about it here: https://stackoverflow.com/questions/252489/why-was-the-switch-statement-designed-to-need-a-break

That said when we added the switch operations we did talk about adding the option to enable the fallthrough behavior as a parameter to the switch operation if it turned out people would need it.