Iterators for D

Sean Kelly sean at f4.ca
Mon Nov 6 13:18:55 PST 2006


Walter Bright wrote:
> It's becoming increasingly obvious that D needs iterators. While opApply 
>  is far better for some kinds of iteration (such as recursively 
> traversing a directory), iterators are more efficient in some cases, and 
> allow for things that opApply makes difficult.
> 
> So hear are the design goals:
> 
> 1) Works with dynamic and stat arrays
> 2) Doesn't need to work with associative arrays
> 3) Should be possible to achieve "pointer efficiency" with it
> 4) Needs to be able to restrict lvalue access (what C++ does with const 
> iterators)
> 5) Needs to work seamlessly with foreach
> 6) Iterators need to be copyable
> 
> So here's one design:
> 
> ..being property returns an iterator that starts at the beginning
> 
> ..end returns an iterator that is at the end

Is it worth discussing whether the pointer-style C++ iterators are the 
best approach?  They certainly are if random access iterators are a 
requirement, but if we're just implementing forward and reverse 
iterators, the all-in-one Java approach may be better.  In that vein, I 
  assume alose that reverse iteration would work with reverse iterators 
and an .rbegin type method instead of .begin and foreach_reverse?


Sean



More information about the Digitalmars-d mailing list