DEV Community

Discussion on: Language Proposal: The 'Any' Switch Case

Collapse
 
ryanberger profile image
Ryan Berger

So I've thought over this a couple of times, and I may not see the beauty of it, or am missing the benefits, but why couldn't you just put the line of code you want to run either before or after the switch? For example:

You propose:

const myValue = 10;
switch (myValue) {
    case 10:
        doThingForValue10();
        break;
    case 5:
        doThingForValue5();
        break;
    any: // maybe some sort of syntactic sugar to make block run before/after cases are evaluated:
        console.log(myValue);
        console.log("run for all cases");
}

However, this begs the question of when the any block actually runs. There can be lots of discussion on how we should achieve that, but I think it gets messy especially when you want some code to run before AND after. I think that you can write code with the same functionality in vanilla javascript which would look like this:

const myValue = 10;
console.log(myValue); // this would run just the same as it would inside the any block
switch (myValue) {
    case 10:
        doThingForValue10();
        break;
    case 5:
        doThingForValue5();
        break;
}
console.log("run for all cases"); // not only can we choose if it runs before/after, but we can split code that would be inside the any block to run before AND after