Iterators, generators, finally, and scarce resources
I also note this is the first JS construct that would introduce the notion of resource acquisition and also of implicit finallies.
And that is why late changes like this are risky. As far as I know, there's been no published work exploring how this aspect of for-of would interact with future designs which might address resource management in general.
Furthermore, as far as I know we have absolutely no real-world experience with how such an auto-cleanup construct would actually fare in javascript. There is no time to do the homework that this effort would properly require.
On 4/30/14, 3:09 AM, Andy Wingo wrote:
There are no other objects in JS that have a finalization facility or other "shut down" semantics.
This has actually come up as a problem for DOM objects. Some people actually want a semi-generic facility to say "I'm done with you" instead of having to have per-object-type hacks...
And yet many of them can indeed hold onto scarce resources (to the extent that is possible in JS, which is to say, approximately never in the browser)
It happens all the time in the browser. You can hold on to address space (e.g. via huge typed arrays). You can hold on to graphics card resources via WebGL objects. I expect to see more instances of this as we expose more hardware capabilities to the web.
and none of them have similar facilities.
This is certainly true, sadly.
On Tue 29 Apr 2014 20:35, David Herman <dherman at mozilla.com> writes:
There are no other objects in JS that have a finalization facility or other "shut down" semantics. And yet many of them can indeed hold onto scarce resources (to the extent that is possible in JS, which is to say, approximately never in the browser), and none of them have similar facilities.
This truly seems a case of the tail wagging the dog.
This is only one of the consumers of iterators. Should exceptions thrown while destructuring iterators also imply "shutdown"? I also note this is the first JS construct that would introduce the notion of resource acquisition and also of implicit finallies.
Again, it's only the holders of scarce resources that even need to consider this question of "safety", as it is only in their case that the question arises! Normal code -- the common case -- cannot be "unsafe".
Can you elaborate? This is getting quite far afield of the existing drafts :(
next() and throw() also have return values, FWIW...
Again this argument needs to provide more details on the asynchronous case, and as the solution I propose, I think it makes sense -- it's the same code that has the responsibility for cleaning up either way, whether it's implicit, opaque, and not configurable (Jafar's proposal) or explicit and configurable (status quo).
ES7 do-expressions mark their returns with "return" or otherwise via tail position AFAIU. My argument was that making "yield" have three continuations tips the balance for me into bogosity.
Anyway this is going back and forth on nits; I'll just pick a couple more below. I've probably repeated myself enough over the years!
This is true. And as Filip notes, this isn't a fundamental perf problem -- it can be worked around. But it's also true (AFAIK) that no engine optimizes try/finally right now.
It's a question of whether the loop as a whole is ion'd, dfg'd, ftl'd, crankshaft'd, etc or not.
,
Andy