return when desugaring to closures

Waldemar Horwat waldemar at google.com
Tue Oct 14 11:32:48 PDT 2008


David-Sarah Hopwood wrote:
> David-Sarah Hopwood wrote:
>> Waldemar Horwat wrote:
>>> I am talking about let bindings.  Lars brought up at that meeting.  I did not find the use cases particularly convincing, but the dead zone is compelling.  There are four ways to do this:
>>> A1. Lexical dead zone.  References textually prior to a definition in the same block are an error.
>>> A2. Lexical window.  References textually prior to a definition in the same block go to outer scope.
>>> B1. Temporal dead zone.  References temporally prior to a definition in the same block are an error.
>>> B2. Temporal window.  References temporally prior to a definition in the same block go to outer scope.
>>>
>>> Let's take a look at an example:
>>>
>>> let x = "outer";
>>> function g() {return "outer"}
>>>
>>> {
>>>   g();
>>>   function f() { ... x ... g ... g() ... }
>>>   f();
>>>   var t = some_runtime_type;
>>>   const x:t = "inner";
>>>   function g() { ... x ... }
>>>   g();
>>>   f();
>>> }
>>>
>>> B2 is bad because then the x inside g would sometimes refer to "outer" and sometimes to "inner".
>>>
>>> A1 and A2 introduce extra complexity but doesn't solve the problem.
>> I already suggested a solution to the problem based on a refinement of A1:
>>
>> <http://www.mail-archive.com/es-discuss@mozilla.org/msg00899.html>
> 
> This post was more directly relevant:
> <http://www.mail-archive.com/es-discuss@mozilla.org/msg00889.html>

This won't work for many reasons.  Making the scope of a function depend on the details of what it refers to inside is too subtle of a rule to be practical.  You'd need to examine the body of the entire function before you know when you can refer to its name from the outside.  Also, it interacts badly with existing ES3 semantics.

Here's an example:

var f;

f(5);

const x = 2;

function f(a) {
  const g = function() {return x;}
  if (a != 5) g();
  if (false) x;
  return 3;
}

This had better work, or else you're violating ES3 semantics.  There's nothing wrong with the call to f.

    Waldemar


More information about the Es-discuss mailing list