break seen as a C archaism
guy.steele at oracle.com
Thu Mar 15 21:44:04 UTC 2018
Okay, Maurizio, you got me thinking. As long as we are convinced that we are actually going to use an explicit value-returning statement within a switch expression quite infrequently in real code, why don’t we get the best of both worlds by spelling it this way:
break return x;
Then everybody is happy:
(1) Cannot be confused with the old `break` syntax.
(2) Clearly exits a `switch` like `break` does.
(3) Clearly returns a value like `return` does.
(4) Better encourages exclusive use of `->` (because using `->` rather than `: break return` saves even more characters than using `->` rather than `: break`).
(5) In the year 2364, this can be further generalized to allow `continue return x;`.
(6) Those who want new language features to really jump out will surely be satisfied.
> On Mar 15, 2018, at 5:13 PM, Maurizio Cimadamore <maurizio.cimadamore at oracle.com> wrote:
> So, from a language design perspective, 'return x' is wrong - but, as you point out, we already committed the original sin of having 'return == local return' for lambdas, so I'm not too convinced that we couldn't use the same story again here. E.g. when you say 'return', what you really mean is 'returning from the innermost context'. This could be a method (as usual), or a nested expression e.g. a lambda or a switch expression.
> Kevin has a point in that using return is mildly worrisome when it comes to refactoring; but we had exactly the same problem with lambdas when we were considering migrating code using internal iteration (for loop) to code using external iteration (Stream forEach) - again, there the refactoring could not be 100% smooth - if the body of your loop had some abnormally completing branches, then there was no way to translate that into an external iteration idiom - at least not mechanically (e.g. 'return x' already meant something different inside old-style for loop bodies).
> So, seems to me that we end up with the same bag of pros and cons? E.g. more familiar to the user (return <expr> is something that they know and love), but more smelly from a design point of view (in a way that forecloses using 'return' to mean non-local return, but I wonder - has that ship already sailed?)
More information about the amber-spec-observers