4 ms·
Why "might as well"? Things can be optimal in some places and suboptimal elsewhere.
by 88e282102ae2e5b 11y ago
Why "might as well"? Things can be optimal in some places and suboptimal elsewhere.
- lmm 11y agoConsistency is valuable. If some blocks have different rules to other blocks that's a real downside.
- 88e282102ae2e5b 11y agoThat's true, but they're so syntactically different that I can't imagine a scenario where someone is asking whether it's closures or functions that you're supposed to use return statements in.
- Sanddancer 11y agoA foolish consistency is the hobgoblin of little minds. Sometimes a variation in rules allows for more clarity -- one could set their editor to make return a different color, for example, making it easier to glance at program flow. Then again, I'd argue that closures should allow returns too, like they do in C#. I'm an adamant supporter of the explicit camp -- when you have to debug things at 3 in the morning, sometimes that extra little bit of context can make things blindingly obvious.
- pcwalton 11y agoClosures do allow returns in Rust.
- lmm 11y agoI find the way it works in scala very nice - a block is just an expression that evaluates to the last statement in the block, and idiomatic code never uses "return" anywhere. Even e.g. a function definition, you can replace the block with a single expression if it's more convenient. I agree with being explicit but I don't think return actually makes things any more explicit (if you're talking about an editor highlighting, the editor knows which line is the value of the block, with or without a "return") - rather it's just syntactic ceremony. It's surprising how much difference having very low-overhead closures makes - you can make so much of your program simpler, because it's not a problem if a caller needs to make a slight modification to something.