3 ms·
> just to make the whole "you don't need to say return in the last statement" look clever. I sure prefer `let x = if foo { 0 } else { 1 };` over `let x = if fo
by alpaca128 4y ago
> just to make the whole "you don't need to say return in the last statement" look clever.
I sure prefer `let x = if foo { 0 } else { 1 };` over `let x = if foo { return 0; } else { return 1; };`. Which raises another potential dilemma: do you break consistency and allow returning values without `return` in if-blocks, or do you make it impossible to return from a function early within them, instead making the returns exit the if-expression early for ultimate confusion?
No, please don't take away my semicolons unless you provide an actual alternative, even if it's just a Lisp-like flood of parentheses.
- xigoi 4y agoWhy not do it like Ruby, where blocks always return the result of the last statement (and the return keyword returns from the nearest enclosing function)?
- FullyFunctional 4y agoI think the alternative style of semicolons are as separators which makes more sense from the grammar and is what many languages that are C-like do. Rust tries to be both and unsurprisingly ends up with complicated and IMO fragile rules.
- stormbrew 4y agoIt's not clear to me why it even needs to be treated as a special case. In particular, the biggest other example of a language that has this "return or hanging last expr" I'm aware of is ruby, where the problem with it is that accidentally returning a value from a block might affect escape analysis for GC, but rust doesn't have that problem. In your example, the if is in expression position, so it's fine if it treats its bodies as expressions. I'm definitely not saying I want to pepper my code with returns, I'm just unconvinced semicolons help with avoiding that.
- foerbert 4y agoThere are other options out there. Ada, for example, has extended return statements and - more directly - expression functions.