Immediate closing of iterators

Brendan Eich brendan at mozilla.org
Thu Dec 21 09:06:48 PST 2006


On Dec 21, 2006, at 3:18 AM, Chris Hansen wrote:

> This means that you can't even delegate a call that returns a
> generator without voiding this guarantee?
>
> class CollectionWrapper {
>  var collection = ...some collection...;
>  function gen() {
>    return this.collection.gen();
>  }

You're right, the case 2 analysis I wrote up in my last message was  
too restrictive.  If you put a type annotation on function gen, then  
the guarantee should hold:

class CollectionWrapper {
  var collection = ...some collection...;
  function gen():Generator.<*,*,*> {
    return this.collection.gen();
  }
}

(You could use narrower types than * if appropriate.)

 From http://developer.mozilla.org/es4/proposals/ 
iterators_and_generators.html:

"A function containing a yield expression is a generator function,  
which when called binds formal parameters to actual arguments but  
evaluates no part of its body. Instead, it returns a generator- 
iterator of nominal type Generator:

class Generator.<O, I, E> {
   public function send(i: I) : O,
   public function next() : O;
   public function throw(e : E) : O,
   public function close() : void
};
"

So the guarantee has two conditions: that the object to the right of  
'in' in the 'for-in' loop is not an iterator (case 1 in my last  
message); or if it is, that it is a generator-iterator (case 2,  
revised).  A generator-iterator is an instance of Generator.<O,I,E>.

In this light, case 1 is really just an optimization to allow  
finalization of any non-generator iterator created for an iterable o  
given for (i in o), where o has an iterator::get method that's not a  
generator-function. It's not just an early out to calling close,  
because the iterator may not have a close hook (may not be a  
Generator); the benefit is that it can be finalized promptly. This is  
informative not normative, since case 2 would uphold the guarantee. I  
wasn't clear last time, sorry about that.

/be



More information about the Es4-discuss mailing list