3 ms·
I didn't quite grok this bit: > The ability to return values from if expressions and blocks is awesome and I don’t know how I’ve managed to live up until now w
by mrec 7y ago
I didn't quite grok this bit:
> The ability to return values from if expressions and blocks is awesome and I don’t know how I’ve managed to live up until now without it. Instead of conditionally assigning to a bunch of variables it is better to return them from an if expression instead.
The example shown (aside from the fact it's assigning a tuple, which is a different point) would naturally be a ternary in C/C++. Does the awesomeness kick in for more complicated examples where you want intermediate vars etc in the two branches?
- monocasa 7y agoYeah, exactly. YOu know how it becomes a pain to make a const variable that needs some setup in C and C++? This lets you const all the things.
- flohofwoe 7y agoYes, anything which is more complex than a very simple if-else. The advantage is even more obvious with switch-expressions, e.g.: https://ziglang.org/documentation/master/#switch https://ziglang.org/documentation/master/#switch https://doc.rust-lang.org/reference/expressions/match-expr.html https://doc.rust-lang.org/reference/expressions/match-expr.h...
- gameswithgo 7y agoThere are lots of little nice things: * you can more easily handle more than 2 options * you can more easily have a bunch of code in each case * you don't have a new syntax for the compiler or programmer to have to know about, everything is just consistently an expression It is nice, and those of us who say that are usually aware of various alternative workarounds with ternaries etc.