Iterators for D

Walter Bright newshound at digitalmars.com
Mon Nov 6 12:46:01 PST 2006


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.



More information about the Digitalmars-d mailing list