3 ms·
Go doesn’t have a ternary operator, you are supposed to write something like boo := bar if foo { boo = baz } One of the many cases where G
by School-Cotton 2y ago
Go doesn’t have a ternary operator, you are supposed to write something like
boo := bar
if foo {
boo = baz
}
One of the many cases where Go’s designers decided they would ban something they disliked about C (in this case, complicated ternary operator chains), but thought Google programmers were too stupid to understand any idea from a more modern language than C, so didn’t add any replacement.
(I’m not exaggerating or being flippant: Google programmers being too stupid to understand modern programming languages has literally been cited as one of the main design goals of Go).
- lyu07282 2y agoThe second you add a tenary operator people are gonna nest them, but the same is true for if/switch/match expressions unfortunately. I don't think they meant stupid literally, it's more like KISS philosophy applied to language design for maintainablity/readability/code quality reasons. Google employs some of the smartest programmers in the world.
- School-Cotton 2y agoNesting them is not so bad if the syntax makes it obvious what the precedence is, which isn't true of C, but is of Rust for example. Anyway, complicated code should be avoided whenever possible, true, but banning the ternary operator (and similar constructs like match/switch statements as expressions) does nothing to make code simpler. It just forces you to transform let x = (some complicated nested expression); into var x; // (some complicated nested tree of statements where `x` is set conditionally in several different places)