Hacker News new | ask | show | jobs
by edflsafoiewq 2264 days ago
A switch is a kind of computed goto. It's a fantastically useful construct when you need it. You don't usually need it. I'm not sure what mini-syntax you're referring to, the syntax is roughly what I would have guessed it would look in C if I hadn't seen it and you just told me its semantics.

Most people treat switch as a peculiar form of match (or cond or whatever), probably because this would usually be much more useful to have than a computed goto, and in many derivative languages they've put in weird additions that seem to be an attempt to drag it halfway to that, things that don't really make any sense in C, like allowing dynamic expressions in the cases (such as your example), or forcing all cases to be at the top level.

(I feel like I write a lot of "in defense of switch" comments on HN...)

1 comments

> Most people treat switch as a peculiar form of match (or cond or whatever)

Completely made up switch illustrates how I use it in every language if I can...Go has a succinct version of this.

    switch(True) {
        case ($x == 1):
            $x++;
            break;
        case ($x > 3):
            $x--;
            break;
        default:
            break;
    }
Why? It's longer, more complex, and more highly indented than a normal if-elif-else.