Generator issue: exceptions while initializing arguments

Dmitry Soshnikov dmitry.soshnikov at gmail.com
Mon Sep 10 17:55:50 PDT 2012


On Mon, Sep 10, 2012 at 5:38 PM, Brendan Eich <brendan at mozilla.com> wrote:

> Dmitry Soshnikov wrote:
>
>> FOO = 45;
>>
>> bar(); // 48
>>
>> Use cases like:
>>
>> function bar(y = FOO + 3) {
>>   var FOO = 10;
>>   return y;
>> }
>>
>> May look subtle, however, if again to accept lexical scope as the
>> function definition, then FOO again should be resolved in the
>> bar.[[Scope]], not in the bar's activation:
>>
>> bar(); // still 48
>>
>
> It's a good point. The ES6 proposal is more expressive than Python and
> subsumes it. You can compose:
>
>   var FOO = 42;
>   function bar(x, y = FOO + x) {...}
>
> and use both activation-scope parameters and outer-scope closed-over
> variables as usual.
>
>
Yes, and it sounds interesting. However, then we may end up with
complicated rules for default expression resolutions.

So, logically, "FOO" should be taken from the outer scope:

var FOO = 42;
function bar(x, y = FOO) { ... }

However, then if we take the case with accessing the activation frame as
well:

var FOO = 42;
function bar(x, y = FOO + x) { ... }

then we need to described more complicated algorithms of defaults
resolutions to cover these use cases:

var FOO = 42;
function bar(x, y = FOO) { var FOO = 10; }

bar(); // 42

aka "try to resolve in bar.[[Scope]], if not found, then go the activation
frame". So in this case:

function bar(x, y = FOO) { var FOO = 10; }

bar(); // it should be 10 then, since isn't found in the bar.[[Scope]]

Or even more confusing:

var FOO = 42;
function bar(x, y = FOO + FOO) { var FOO = 10; } // wait ..wha..?

by the rules from above it should be 84, since found in the bar.[[Scope]],
or 20 if FOO doesn't exists above.

So to avoid such complex rules, I'd stick with only parent scope.

Or, if it's desirable nevertheless to have the ability to refer args, then
to reduce only to them, not to the inner variables:

var FOO = 42;
function bar(x, y = FOO + x) { ... } // OK

But not this one:

var FOO = 42;
function bar(x, y = FOO + y) { var y = 10; } // Error .. ?

This will make semantic difference b/w vars and locals though.

So, the simplest and straightforward resolution seems to me as the parent
scope only, w/o referring to the locals/args.

Dmitry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120910/79785250/attachment-0001.html>


More information about the es-discuss mailing list