Yield as contextual keyword

Brian Goetz brian.goetz at oracle.com
Fri May 24 15:24:32 UTC 2019


Right.  So far we have only talked about the language spec and the meaning of programs.  There is lots more that compilers and ides can do to help users steer away from the (actually quite small) puzzler zones. 

Sent from my iPad

> On May 24, 2019, at 10:53 AM, Guy Steele <guy.steele at oracle.com> wrote:
> 
> 
>> On May 24, 2019, at 10:34 AM, Brian Goetz <brian.goetz at oracle.com> wrote:
>> 
>> 
>> var yield = 5;
>> 
>> yield is lexed as an identifier, so this is a valid variable declaration
>> 
>> var res = switch(yield) {
>> 
>> yield is lexed as an identifier, so this is a valid switch operand
>> 
>> 
>>    default -> yield + yield;
>> 
>> RHS of a single-consequence case is expression | block statement | throw.  We're not in a YieldStatement production, so this is an expression.
>> 
>> Like lambdas, we can think of this as shorthand for
>> 
>>    default -> {
>>        yield yield + yield;
>>    }
>> 
>> which parses as a yield statement whose operand is the expression yield+yield.
> 
> And, yay (thanks, Tagir),
> 
>    default -> {
>        yield + yield;
>    }
> 
> is a genuine, bona fide puzzler!
> 
> (But as long as we are doing special-case parsing and checking the next token right after an occurrence of yield, it is a puzzler we can easily defend against if we want to.)
> 
> —Guy
> 



More information about the amber-spec-observers mailing list