How do I make my class iterable?

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 22 13:33:59 PDT 2015


On Monday, 22 June 2015 at 18:44:22 UTC, Assembly wrote:
> I'm using this, thanks for all. Can someone clarify how does 
> opApply() works? I assume it's called every iteration and as 
> opApply() has a loop does it means the number of iteration ran 
> actually is the ones from foreach() is 2*n where n is the 
> number of elements in the array, is this right?

opApply is called only once. The body of the foreach is passed to 
opApply as a delegate. The opApply implementation runs the 
supplied foreach body, possibly in a loop. Every such call is an 
iteration of the foreach loop.

With opApply this:
     foreach(x; iterable) {/* do something with x */}
gets rewritten to this:
     iterable.opApply((x) {/* do something with x */});


More information about the Digitalmars-d-learn mailing list