Call for bikeshed -- break replacement in expression switch

Remi Forax forax at univ-mlv.fr
Fri May 17 06:30:21 UTC 2019



----- Mail original -----
> De: "John Rose" <john.r.rose at oracle.com>
> À: "Alex Buckley" <alex.buckley at oracle.com>
> Cc: "amber-spec-experts" <amber-spec-experts at openjdk.java.net>
> Envoyé: Vendredi 17 Mai 2019 03:15:36
> Objet: Re: Call for bikeshed -- break replacement in expression switch

> On May 16, 2019, at 2:43 PM, Alex Buckley <alex.buckley at oracle.com> wrote:
>> 
>> If you're proposing to disallow a cast expression or a parenthesized expression
>> after a `yield` token, then I think that's not right. The parsing of a `(`
>> token has triggered potentially unbounded lookahead for some time [1][2], and
>> everything worked out, so I don't see why the language should disallow any of
>> John's examples:
>> 
>> yield (String)("answer is "+x);
>> yield ("answer is "+x).trim();
>> yield new String[]{ "answer is "+x }[0];
>> yield Arrays.asList("answer is "+x).get(0);
>> yield false ? 0 : ("answer is "+x).trim();
> 
> Here's what's tricky:  If there is a method called "yield"
> in scope, then one of those examples is a valid method
> call expression statement.
> 
> import static MyFavYielder.yield;
> class Client extends MaybeHasYieldMethod {
> void m(int x) {
> var res = switch (x) {
> case 42 -> {
>  yield ("answer is "+x).trim();
> }
> default -> -1;
> }}
> 
> Here's one way to slice it (very thin):
> 
> The name "yield" is placed in scope in "->"
> blocks as if it were an inherited or imported
> static method.  It acts like an arity-1
> signature-poly method returning void.
> When "yield" is followed by a paren,
> an appeal to this method, and any
> other ambient methods named "yield"
> is made, and overloading and ambiguity
> analysis is done.  If after all the special
> sig-poly method is matched, then the
> compiler edits the statement into a
> control flow construct.
> 
> (This is circular:  A control flow construct
> affects ambient DA/DU rules which might
> also indirectly affect types IIRC.  So the type
> of the yield call maybe could circularly depend
> on the surrounding control flow.)

I would prefer a more "brutal approach" for the shake of my brain, i would like those rules to be true:
- inside a -> block, the "yield" text always means yield from that block.
- if there is no -> block (no switch expression), the compiler will not emit an error. 

It works that way, at the beginning of a -> block, the compiler checks in the scope if there is a method named "yield" available (whatever the number of parameters), if it's true, the compiler reports an error.
This rule is voluntarily simple, so a human can understand it :)

And if there is an unqualified access to a method yield anywhere in the compilation unit, the compiler emits a warning to help users to change their code to make it more readable.

[...]

> 
> — John

Rémi


More information about the amber-spec-experts mailing list