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. -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.
