Call for bikeshed -- break replacement in expression switch

Brian Goetz brian.goetz at oracle.com
Thu May 16 20:10:24 UTC 2019


While dodging the arrow, I’ll point out that there is a pleasant ambiguity in the following:

    x = switch (y) { 
        case L -> { 
            foo();
            yield 7;
       }
   };

Does the `yield` yield a value to the _block_, or to the _switch_?  Answer: IT DOESN”T MATTER!  Whichever intuition feels comfortable to you, yields the right answer.  If we think of it as yielding to the block, then the block terminates normally with 7, and therefore the case label does, and therefore the switch does.  If we think of it as yielding to the switch, then the switch completes normally with 7.  And if we later want to expand block expressions to more places, maybe with some new syntax, then in a future Java

    case L -> { … }

becomes sugar for

    case L -> BLOCK_COMING { … }

at which point the yield is retconned to yield to the block.  

> On May 16, 2019, at 3:58 PM, John Rose <john.r.rose at oracle.com> wrote:
> 
> On May 16, 2019, at 12:36 PM, Alex Buckley <alex.buckley at oracle.com> wrote:
>> having `yield` as the junior member of that club is quite natural. Putting the junior and senior members side by side shows both similarity and difference:
> 
> If junior yield is allowed to help senior return with
> his job, we have a more uniform rule:  yield always
> matches an arrow.
> 
> If junior yield should stay off of senior return's grass,
> we have a somewhat less uniform rule:  yield always
> matches an arrow, unless the arrow is coterminous
> with a method body, in which case return must be
> used.
> 
> Either way is OK with me, but the more uniform rule
> seems to give me more insight into what's really
> happening.
> 
> — John



More information about the amber-spec-experts mailing list