How do I make my class iterable?

Assembly via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 22 11:44:20 PDT 2015


On Monday, 22 June 2015 at 17:09:16 UTC, Steven Schveighoffer 
wrote:
> On 6/22/15 1:03 PM, Assembly wrote:
>> [...]
>
> TBH, opApply is much better suited to classes.
>
> But in order to have multiple parameters with foreach by using 
> a range, you must return a tuple:
>
> auto front() { import std.typecons: tuple; return tuple(index, 
> _app.data[index]);}
>
> Note, do NOT do this on your class, you should be creating a 
> range struct type, and return the range from opIndex() with no 
> parameters.
>
> -Steve

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?


More information about the Digitalmars-d-learn mailing list