How do I make my class iterable?
Assembly via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Jun 22 11:48:39 PDT 2015
On Monday, 22 June 2015 at 18:07:36 UTC, Ali Çehreli wrote:
> 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
if my opApply() is defiend as the following:
int opApply(int delegate(ref int, ref T) del)
{
int result;
foreach(int i, T val; _app.data)
{
result = del(i, _app.data[i]);
if(result)
break;
}
return result;
}
and called like:
foreach(int i, MyType p; places) {
isn't i the loop counter?
More information about the Digitalmars-d-learn
mailing list