Treatment of nested 'break'
forax at univ-mlv.fr
forax at univ-mlv.fr
Mon May 14 12:47:55 UTC 2018
> De: "Brian Goetz" <brian.goetz at oracle.com>
> À: "Remi Forax" <forax at univ-mlv.fr>
> Cc: "Kevin Bourrillion" <kevinb at google.com>, "Guy Steele"
> <guy.steele at oracle.com>, "amber-spec-experts"
> <amber-spec-experts at openjdk.java.net>
> Envoyé: Vendredi 11 Mai 2018 02:25:47
> Objet: Re: Treatment of nested 'break'
> While “no new keywords” is not a hard and fast rule, remember that the cost of
> adding conditional keywords is between 10x and 1000x of that of reusing an
> existing keyword (depending on where it occurs in the grammar.) So its fine to
> suggest alternatives, but bear in mind that alternatives are by no means
> cost-free. (When we suggest new keywords, we’ve already done the cost analysis
> to ensure its at the low end of the spectrum.)
yes,
at the same time, people use keywords as a gateway for the semantics, so re-using a keyword with a different semantics may cost a lot in term of debugging time.
Anyway, sleeping on this problem the last night remember me that there is another view of this issue.
One thing i've remarked with the introduction of 'var' is that writing expression oriented codes helps a lot,
by example, instead of
List<String> list;
if (foo) {
list = ...
} else {
list = ...
}
it's easier to write
var list = (foo)? ...: ...;
there is another construction in Java apart switch that may need an expression version, the try syntax (and its brother the try-with-resources)
using the same example as above, instead of
List<String> list;
try {
list = ...
} catch(IOException e) {
list = ...
}
it's easier to write
var list = try { break ... } catch(IOException e) { break ... };
My fear is that if we use break as identifier, it works for 'switch' but not that well for 'try'.
I do not suggest that we should implement try expression, just that 'break' makes less sense if one time we want to retrofit try to introduce a try expression.
regards,
Rémi
>> On May 10, 2018, at 7:20 PM, Remi Forax < [ mailto:forax at univ-mlv.fr |
>> forax at univ-mlv.fr ] > wrote:
>>> De: "Brian Goetz" < [ mailto:brian.goetz at oracle.com | brian.goetz at oracle.com ] >
>>> À: "Kevin Bourrillion" < [ mailto:kevinb at google.com | kevinb at google.com ] >,
>>> "Guy Steele" < [ mailto:guy.steele at oracle.com | guy.steele at oracle.com ] >
>>> Cc: "amber-spec-experts" < [ mailto:amber-spec-experts at openjdk.java.net |
>>> amber-spec-experts at openjdk.java.net ] >
>>> Envoyé: Jeudi 10 Mai 2018 22:04:15
>>> Objet: Re: Treatment of nested 'break'
>>> This might not help, but perhaps think of it as a compound keyword; "break
>>> switch" is not "break with an argument of switch", but a multi-word keyword
>>> itself.
>>> (Back in lambda, when we explored the consequence of using "return" in lambda,
>>> and observed it foreclosed on nonlocal return should we ever want to get there,
>>> we briefly discussed "long return" as a compound keyword for that case. Same
>>> game.)
>>> Personally if I saw "break while", I think I'd immediately know what that means,
>>> and might even thank the author for being clear.
>> You can also use any other word instead of break or break switch.
>> By example,
>> breakswitch 3;
>> Rémi
>>> On 5/10/2018 3:57 PM, Kevin Bourrillion wrote:
>>>> I'm just going to say that naming a keyword as the argument of another keyword
>>>> seems novel and unprecedented for Java, and as such I think should require
>>>> pretty strong justification.
>>>> On Thu, May 10, 2018 at 12:12 PM, Guy Steele < [ mailto:guy.steele at oracle.com |
>>>> guy.steele at oracle.com ] > wrote:
>>>>>> On May 10, 2018, at 3:06 PM, Brian Goetz < [ mailto:brian.goetz at oracle.com |
>>>>> > brian.goetz at oracle.com ] > wrote:
>>>>>>> I think these are both valid explanations, with different outcomes, but anyway
>>>>>>> it's fair to say that it would be confusing to have the latter perspective and
>>>>> >> then try to explain how a value break can get past a surrounding 'for' loop.
>>>>>> One option is: you can't. While I agree there is code that one might like to
>>>>>> write that is made cumbersome by this, it's a valid option, and not one that is
>>>>> > utterly terrible.
>>>>>> Another option is to extend the break syntax along the lines of the proposed
>>>>>> continue syntax. Suppose for every continuable construct x (for, while, switch)
>>>>>> we supported "continue x". So for every breakable construct y we could support
>>>>>> "break y". If a for loop were enclosed in an expression switch, you could then
>>>>> > say "break switch e". Then
>>>>> > if (foo)
>>>>> > break;
>>>>> > else
>>>>> > break 3;
>>>>> > becomes
>>>>> > if (foo)
>>>>> > break for;
>>>>> > else
>>>>> > break switch 3;
>>>>> > and it is much more obvious what is going on.
>>>>> If we are willing to pile up keywords in that manner, an alternate possibility
>>>>> is to spell a value-returning break in a different way:
>>>>> return switch <expression>;
>>>>> Then your example can become (I have added the implicit context):
>>>>> switch (…) { case 17 -> {
>>>>> …
>>>>> for (…) {
>>>>> ...
>>>>> if (foo)
>>>>> break;
>>>>> else
>>>>> return switch 3;
>>>>> … }
>>>>> … }
>>>>> … }
>>>>> The additional advantage of this approach is that it completely eliminates the
>>>>> syntactic ambiguity between
>>>>> break variableName;
>>>>> and
>>>>> break labelName;
>>>>> Given that we think most occurrences of “return switch” (or “switch return”,
>>>>> take your pick) will be abbreviated by -> anyway, this might be an acceptable
>>>>> approach.
>>>>> You can then still choose to go ahead and also allow things like
>>>>> break for;
>>>>> break switch;
>>>>> break while;
>>>>> continue for;
>>>>> continue switch;
>>>>> but that can be a separate decision; these become simply a way to avoid using
>>>>> statement labels.
>>>>> —Guy
>>>> --
>>>> Kevin Bourrillion | Java Librarian | Google, Inc. | [ mailto:kevinb at google.com |
>>>> kevinb at google.com ]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20180514/869fd458/attachment.html>
More information about the amber-spec-experts
mailing list