Proposal to simplify Generators impact

Brendan Eich brendan at mozilla.com
Sat Oct 26 18:00:39 PDT 2013


Yup.

Look (Licio), we've been over this many times, recorded here in 
es-discuss (directly in posts and in TC39 meeting notes). We are not 
adding generator syntax lightly. It is necessary to preserve backward 
compatibility.

/be

> Till Schneidereit <mailto:till at tillschneidereit.net>
> October 26, 2013 8:19 PM
> On Sun, Oct 27, 2013 at 1:59 AM, Lucio Tato <luciotato at gmail.com 
> <mailto:luciotato at gmail.com>> wrote:
> yield*(curr);
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
> Lucio Tato <mailto:luciotato at gmail.com>
> October 26, 2013 7:59 PM
> Rick: I understand. But it is always a trade-off.
>
> If the reason to introduce a new construct is because there may 
> already be code that defines a function called `yield`, it seems to me 
> as a bad trade-off. (advantages vs disadvantages)
>
> In your example...
> function yield() {... <- will raise a parsing error.
>
> Anyway, there are other ways to solve that.
> You can put the asterisk in "yield" instead of the important 
> "function". It's a lot less confusing.
>
> function fibonacci() {
> let [prev, curr] = [0, 1];
> for (;;) {
> [prev, curr] = [curr, prev + curr];
> yield*(curr);
> }
> }
>
>
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
> Rick Waldron <mailto:waldron.rick at gmail.com>
> October 26, 2013 3:08 PM
>
>
>
> On Sat, Oct 26, 2013 at 1:01 PM, Lucio Tato <luciotato at gmail.com 
> <mailto:luciotato at gmail.com>> wrote:
>
>     It's really needed to make js syntax more complex in order to
>     implement generators?
>     It's function* really needed?
>
> Yes, because `yield` is only reserved in strict mode code, which means 
> this is valid today:
>
>   function g() { yield = 1; return yield; }
>
> Since the starred generator function is a new syntactic form, there is 
> no existing code that it can possibly break by making yield a keyword.
>
>     can you just expose "Generator" as a core function?
>     can "yield" be a function-call-like-construct instead of a new
>     language construction?
>
>
> No, because there may already be code that defines a function called 
> `yield`, which would be broken if suddenly yield was a special 
> language-owned function. Consider this:
>
>   // your code defines this...
>   function yield() { return Number.MAX_VALUE; }
>
>   // you then include my library, which exposes this fibonacci():
>
>     function  fibonacci()  {
>          let  [prev,  curr]  =  [0,  1];
>          for  (;;)  {
>              [prev,  curr]  =  [curr,  prev  +  curr];
>              yield(curr);
>          }
>     }
>
>
> What does yield() do? It returns Number.MAX_VALUE every time.
>
> Rick
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
> Lucio Tato <mailto:luciotato at gmail.com>
> October 26, 2013 1:01 PM
>
> It's really needed to make js syntax more complex in order to 
> implement generators?
> It's function* really needed?
> can you just expose "Generator" as a core function?
> can "yield" be a function-call-like-construct instead of a new 
> language construction?
>
> function fibonacci() {
> let [prev, curr] = [0, 1];
> for (;;) {
> [prev, curr] = [curr, prev + curr];
> yield(curr);
> }
> }
>
> Generators can be iterated over in loops:
>
> for (n of new Generator(fibonacci) {
> // truncate the sequence at 1000
> if (n > 1000)
> break;
> print(n);
> }
>
> Generators are iterators:
>
> let seq = new Generator(fibonacci);
> print(seq.next()); // 1
> print(seq.next()); // 2
> print(seq.next()); // 3
> print(seq.next()); // 5
> print(seq.next()); // 8
>
> Advantages: No new syntax, no "function*" (Cognitive dissonance, every 
> good C programmer can't stop reading function* as "function pointer"). 
> No "yield" new construction. No added complexity to the language syntax.
>
> By not adding new elements and complexity to the language, you're 
> keeping it consistent.
>
> By using "new Generator(fn)" to create a Object-Generator, you can 
> also expose a "done" property and other useful info in a standard way.
>
> Note: why yield syntax should be like a function call:
> In actual implementations (V8 --harmony) "yield" will "return" the 
> value passed in the call to ".next"
>
> functiongiveNext(){//generator
> letactual=10;
> while(actual<100){
> skip=yield(actual);
> actual=actual+skip||5;
> };
> };
>
> letseq=newGenerator(giveNext);
> print(seq.next());// 10
> print(seq.next());// 15
> print(seq.next(20));// 35
> print(seq.next());/// 40
> //
> /*let*seq=newGenerator(giveNext);
> while (!seq.done)
> print(seq.next());
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss


More information about the es-discuss mailing list