return when desugaring to closures

Brendan Eich brendan at mozilla.org
Fri Oct 10 15:31:59 PDT 2008


On Oct 10, 2008, at 1:25 PM, Waldemar Horwat wrote:

> So what should f(5, 0) do?
>
> function f(x, h) {
>  while (true) {
>    try {
>      if (h == 0)
>        h = function() {break};

Just to repeat something Dave wrote, we don't propose to allow break  
in a function where the break is not in a labeled statement, switch,  
or loop within that function. Only lambda would support such novelties.


>
>      if (x != 0)
>        f(x-1, h);
>      else
>        h();

This will break from the while (true) in the outermost (x = 5)  
activation of f.

In Scheme implementations that support it, the analogue is call/ec --  
call-with-escape-continuation (weak continuation is another name for  
escape continuation) -- where the caller's continuation is the  
argument to the procedure passed to call/ec. Escape continuations are  
cheaper to implement and simpler to reason about than full call/cc  
continuations because of the dynamic error (exception) you get if you  
call the escape continuation outside of the dynamic extent of the  
current call.

Sorry if this is already known; Dave should wipe my chin as needed  
since he is the adult Schemer here and I'm the toddler.


>    } catch (e) {
>      alert("caught " + e + " on " + x);
>    } finally {
>      alert("f called finally on " + x);
>    }
>    alert("f looping on " + x);
>  }
>  alert("f exited on " + x);
> }

The break itself does not propagate as an exception, just to be clear.  
If the statement being broken from is inactive, then an exception will  
be thrown from the break evaluation in the function that was assigned  
to h. The call to h would have to come after control flow had left the  
outermost while (true), via a statement after that loop, or some other  
call made via a returned or heap reference (upward funarg).

/be


More information about the Es-discuss mailing list