Generator issue: exceptions while initializing arguments
Dmitry Soshnikov
dmitry.soshnikov at gmail.com
Tue Sep 11 11:37:00 PDT 2012
On Tue, Sep 11, 2012 at 1:33 AM, Brendan Eich <brendan at mozilla.com> wrote:
>
> If you really want the Pythonic default parameter rules (evaluation in
> definition context) then you need to address the bad case Jason showed:
>
> function foo(arg = []) {
> arg.push('strange');
> return arg;
> }
> foo(); // ['strange']
> foo(); // ['strange', 'strange']
>
> This is a side channel and a big footgun. ES6 default parameters as
> proposed avoid it.
>
>
Sure, and as I mentioned in my first message -- ES may eval defaults every
time at activation, not only once as Python does. But -- still in the outer
scope (in the foo.[[Scope]] in your example).
function foo(arg = <expression>) { ... }
evaluates the <expression> every time at activation as:
1. If expression is a literal value, create a *new* value corresponding to
literal. Assign to the parameter. Return.
2 Else, eval the <expression> in foo.[[Scope]]
The first step covers and fixes Python's issue. The second allows not going
to the activation frame and avoid complexities I noted.
That's said, we should stick either with outer, or with the inner scope for
evaluation. The outer seems more logical. Not with both, otherwise it will
be too complicated and error prone use cases (which as I note will anyway
be banned as a bad practice, so to support it?).
Dmitry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120911/9ea9b6ee/attachment.html>
More information about the es-discuss
mailing list