I think Tim means that the author of the generator should assume it will be suspended at any time, not the consumer.
On Aug 6, 2013, at 3:17PM, Mark Hahn <[email protected]> wrote: > > Always assume that you will be suspended any time you have a yield or > > yield* > > How do you know any of this you see a function call? Only the function > definition looks different. Tell me which is these will yield .. > > a() > b() > c() > > On Tue, Aug 6, 2013 at 2:54 PM, Tim Caswell <[email protected]> wrote: > > > > On Tue, Aug 6, 2013 at 4:47 PM, Mikeal Rogers <[email protected]> wrote: > It is the *exact same* hazard as the code you posted, I don't think it is any > *more* of a hazard at the language level. The difference is that the pattern > in the function code below is basically never seen in node.js code and it has > been *possible* to write it since node's creation. > > Could be that nobody will use/clobber closure scope in their generators > either, but I'm afraid they will because it will work and operate as expected > *most* of the time and, unlike the function example you've posted, it will > still fit well when used with what I assume will become a healthy ecosystem > of generator consuming libraries. > > The reason nobody writes this kind of thing in node is that it just won't > play well with the rest of the libraries in the ecosystem whereas the > hazardous generator example looks like it would fit just as well as a "well > written" generator until it hits scale and causes visible bugs. > > All kinds of bad patterns and bad code are possible in any language. The > patterns we adopt to create a healthy ecosystem are what will keep people > away from the third rails. Depending on people to have "well written" code, > by some definition of "well", is not a good alternative to solidifying and > encouraging patterns that make it *difficult* to write hazardous code in the > first place. > > No pattern or feature in JavaScript escapes the potential for abuse and bugs, > and nothing that I'm worried about w/ respect to generators is worse or > better than language level hazards w/ callbacks, it's just that we've created > a set of patterns that *discourage* those hazards in the current ecosystem > and I'm failing to see how we do that in this particular case with generators. > > I propose a simple rule for all generator code: > > - Always assume that you will be suspended any time you have a yield or > yield* and any shared state may change. > > If we educate everyone and focus on that one thing (yield) that will cover > all hazards around shared state and generators, be it async > promise/continuable based stuff or sync lazy iterator stuff. > > Just like with functions we've learned to look for the "function" keyword and > guard our state around that point, we need to do the same around the "yield" > keyword. > > Teach people correct principles and trust them to do the right thing. We'll > be fine. > > > > -Mikeal > > On Aug 6, 2013, at 2:33PM, Tim Caswell <[email protected]> wrote: > >> My question to people on the list is how is this any different than the same >> hazard with normal function? >> >> var sum, i; >> function reduce(arr) { >> sum = 0; >> i = 0; >> return function () { >> if (i < arr.length) { >> sum += arr[i++]; >> return sum; >> } >> }; >> } >> > > > -- > -- > Job Board: http://jobs.nodejs.org/ > Posting guidelines: > https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines > You received this message because you are subscribed to the Google > Groups "nodejs" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/nodejs?hl=en?hl=en > > --- > You received this message because you are subscribed to the Google Groups > "nodejs" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/groups/opt_out. > > > > > -- > -- > Job Board: http://jobs.nodejs.org/ > Posting guidelines: > https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines > You received this message because you are subscribed to the Google > Groups "nodejs" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/nodejs?hl=en?hl=en > > --- > You received this message because you are subscribed to the Google Groups > "nodejs" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/groups/opt_out. > > > > > -- > -- > Job Board: http://jobs.nodejs.org/ > Posting guidelines: > https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines > You received this message because you are subscribed to the Google > Groups "nodejs" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/nodejs?hl=en?hl=en > > --- > You received this message because you are subscribed to the Google Groups > "nodejs" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/groups/opt_out. > > -- -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en --- You received this message because you are subscribed to the Google Groups "nodejs" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/groups/opt_out.
