Generator issue: exceptions while initializing arguments

Brendan Eich brendan at mozilla.com
Tue Sep 11 02:15:32 PDT 2012


Domenic Denicola wrote:
> On Sep 11, 2012, at 4:39, "Brendan Eich"<brendan at mozilla.com>  wrote:
>
>> Brendan Eich wrote:
>>> Dmitry Soshnikov wrote:
>>>> var FOO = 42;
>>>> function bar(x, y = FOO + z) { var z = 10; } // Error .. ?
>>> Translating to JS as it is:
>>>
>>>   var FOO = 42;
>>>   function bar(x, y) {
>>>     if (y === void 0) y = FOO + z;
>>>     var z = 10;
>>>   }
>> Another benefit of the simple semantics: existing JITs can optimize easily, no new semantics to special-case. This is not a dominant concern but it is a selling point from my experience dealing with (and playing one on TV ;-)) implementors.
>>
>> /be
>
> And, if one replaced all `var`s in the above example with `let`s, one would get the desired error, right?

Let's see:

  var FOO = 42;
  function bar(x, y) {
    if (y === void 0) y = FOO + z;
    let z = 10;
  }


Yup, in the case of y missing or passed explicit undefined value, z is 
used before initialized, so in its temporal dead zone => error.

>   To me the most important use cases to consider are those in an "idiomatic ES6" let-is-the-new-var world.

Sure, although as I keep arguing, formal parameters are var-like at 
least because compatibility requires us to keep this working:

   function f(x) { var x; return x }

as the identity function. Whereas

   function f() { let x; var x; }

is an early error.

I've also argued that arguments aliasing in non-strict functions makes 
parameters var not let, but Allen countered that arguments can be 
magical no matter how we model parameter binding. To be explicit

js> function f() {
     let x = 0;
     let magic = {get x() { return x; }, set x(nx) { x = nx; }};
     magic.x = 1;
     return x;
}
js> f()
1
js>


Obviously, x is let-bound but still aliased by magic.x, just as for a 
first formal parameter x aliased by arguments[0].

So Allen's right, and my point becomes aesthetic or pedagogical: I don't 
think we should say formal parameters are let-like given arguments 
aliasing in non-strict functions, but this is a weaker objection, and 
secondary to the main one:

function f(y) {
     let x = y;
     var x;
     return x;
}

in ES6 gets an early error on the var x; line, so

function f(x) {
     var x;
     return x;
}

should too if formal parameters are truly let-based. But we cannot give 
an early error for this last function f, because of backward compatibility.

All of this is probably too subtle for most folks, but it matters at 
least for spec aesthetics/philosophy/coherence.

And it again makes me prefer the simpler, flatter semantics for 
parameter defaults that we've been discussing, whicih (modulo a few edge 
cases) has been prototyped in SpiderMonkey (in Firefox 15).

/be


More information about the es-discuss mailing list