Iterators for D

Benji Smith dlanguage at benjismith.net
Wed Nov 8 09:05:40 PST 2006


Daniel Keep wrote:
> Things that need random access override opIndex and opIndexAssign.
> 
> Things which you can take a range of values from override opSlice and
> opSliceAssign.
> 
> Things that require lock-step iteration override opApply, or supply a
> function that returns a delegate that "runs" a foreach loop.
> 
> About the only thing this *doesn't* cover are bi-directional iterators
> (say, iterating back and forth over an infinite series).

What about containers where there are multiple valid orders of 
iteration? For a binary tree class, I might want to perform iteration 
using depth-first, breadth-first, in-order, or reverse-order traversals. 
How does the opApply/opApplyReverse solution address these needs?

And isn't a delegate-call about as computationally expensive as a 
virtual method call (in the case of an Iterable iterface)?

If the delegate solution is really better than an Iterable interface, 
why create special cases for opApply and opApplyReverse? Why not just 
say that foreach always requires an iteration delegate?

--benji



More information about the Digitalmars-d mailing list