Iterators for D

Dave Dave_member at pathlink.com
Mon Nov 6 13:08:23 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
> 
> Overloading * doesn't work with D. But, instead:
> 
> Overload opIndex for rvalue access
> Overload opIndexAssign for lvalue access
> 
> Overloading opIndex also will work for random access
> 
> foreach loops will not be able to have a 'key' parameter.

Would this mechanism work for, say, arrays of structs (not struct*[] 's) so that we could avoid the 
overhead of either a) copying the struct and/or b) the extra level of indirection in the loop? (In 
other words, could the iterator provide a reference to each element by default).

Thanks,

- Dave



More information about the Digitalmars-d mailing list