Iterators for D

Craig Black cblack at ara.com
Tue Nov 7 08:55:31 PST 2006


"Walter Bright" <newshound at digitalmars.com> wrote in message 
news:eio6u5$1j6o$1 at digitaldaemon.com...
> 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.

It's not a huge problem, but passing an argument needlessly is both ugly and 
slightly inefficient.  Perhaps opIndex and opIndexAssign could be overloaded 
without any parameters.

-Craig 





More information about the Digitalmars-d mailing list