return when desugaring to closures

Brendan Eich brendan at mozilla.org
Thu Oct 9 15:19:53 PDT 2008


On Oct 9, 2008, at 3:05 PM, Lex Spoon wrote:

> On Thu, Oct 9, 2008 at 5:31 PM, Brendan Eich <brendan at mozilla.org>  
> wrote: JS has break from labeled statement, and continue to labeled  
> loop bottom, a la Java. These look trouble-free to me. Let me know  
> if you see a hard case. Thanks,
>
> My question was whether the semantics of break and continue would  
> support the following:
>
> while(true) {
>   (function() {
>     if (--x == 0) break;
>   })();
> }

That's currently a specified error (possibly at runtime; chapter 16 of  
ES3 allows it to be at compile time).

So a future edition could allow it, probably without opt-in  
versioning. Our compatibility model does not guarantee exceptions,  
since it allows contemporaneous extensions that remove those  
exceptions (see ES3 chapter 16 again, second bulleted list, first  
bullet -- these lists need their own sub-sections and numbers!).


> I honestly don't know, but it shouldn't cause any real trouble to  
> allow it.  The implementation would be analogous to that for labeled  
> return.

Right, and break to label outside the function's body, but lexically  
in scope, would be completely analogous (or just "the same" ;-)):

L: while (true) {
   (function () {
     ... // stuff possibly including a loop or switch that brackets  
the next line
     if (--x == 0) break L;
     ...
   })();
}

> For example, if the appropriate while loop is no longer on the  
> stack, the "break" would turn into an exception.

Yes, this new runtime exception is the price of admission.

The exception seems to a major source of grief in the Java BGGA  
closures controversy, or at least it did when I last looked. But it  
comes up with escape continuations in Scheme, and it is inevitable if  
we want these kinds of program equivalences.

I'm interested in the reactions of others on the list to such "return/ 
break/continue from already-deactivated statement/frame" exceptions.  
They could be caught and handled, of course. Feature and bug, dessert  
topping and floor wax ;-).

/be


More information about the Es-discuss mailing list