Generator issue: exceptions while initializing arguments

Brendan Eich brendan at
Mon Sep 10 10:56:20 PDT 2012

Kevin Smith wrote:
> >     function f(x=EXPR1, y=EXPR2) { BODY }
> >     ===>
> >     function f(x, y) {
> >         if (x === void 0) x = EXPR1;
> >         if (y === void 0) y = EXPR2;
> >         BODY
> >     }
>     In case it isn't clear what I'm saying here, I think the same
>     desugaring should hold for generators. Exceptions would then be
>     deferred to "loc 2" in Allen's example.
> I'm not so sure - the desugaring above would mean that default 
> expressions would have visibility across curly-brace boundaries, which 
> I find to be quite surprising.  That's what makes "var" so weird, 
> after all.  The conceptual link between curlies and visibility is 
> inherent in C-like languages.

But as I pointed out, we have problems already in JS:

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

the var x restates the formal parameter. It does not shadow it.

> IMO the expectation will be that default parameter values are 
> evaluated when the generator function is called.

Python "binds" then, but evaluates the expressions at generation 
definition evaluation time.

Expectations may vary, and I still concur with Jason that the simplest 
and thinnest "skin" is best here.

>  Any exceptions that occur in the generator body prior to the first 
> yield will be expected at the first call to next().

That would be loc 2, though -- seems to go against what you wrote earlier.

It also is not how generators work in Python or JS1.7+:

Python 2.6.6 (r266:84292, May 28 2011, 19:08:00)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
 >>> def g():
...   raise BaseException('foo')
...   yield
 >>> i = g()
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "<stdin>", line 2, in g
BaseException: foo

js> function g(){throw 42; yield}
js> i = g()
uncaught exception: 42

The exception comes after the first .next() that starts from the 
implicit yield point.

The design doesn't work if there's no implicit yield first. The rest of 
what's shown above follows from that.


More information about the es-discuss mailing list