How do I make my class iterable?

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 22 11:07:35 PDT 2015


On 06/22/2015 10:03 AM, Assembly wrote:

 > foreach(int i, MyType p; places) {
 >
 > but I get this error:
 >
 > Error: cannot infer argument types, expected 1 argument, not 2

Yeah, the loop counter is automatic only for slices. You can use 
'enumerate' for other types:

     foreach (i, element; NumberRange(42, 47).enumerate) {
         // ...
     }

That is taken from the same page: ;)

 
http://ddili.org/ders/d.en/foreach_opapply.html#ix_foreach_opapply.loop%20counter

Ali



More information about the Digitalmars-d-learn mailing list