4 ms·
I have also seen switch statements used (abused?) to do a similar thing. Since a switch executes the first statement that is equivalent to the control expressio
by Schattenbaer 7y ago
I have also seen switch statements used (abused?) to do a similar thing. Since a switch executes the first statement that is equivalent to the control expression you can pass true as the control expression, e.g.:
switch(true) {
cond1: return 1;
cond2 && cond3: return 10;
//etc
}
Not sure I'd recommend it but it works in a few languages :)
Elixir has cond that does this nicely without resorting to strange switch statements:
https://elixir-lang.org/getting-started/case-cond-and-if.html#cond https://elixir-lang.org/getting-started/case-cond-and-if.htm...