Hi, all.  I find myself a little confused about how foreach, opApply, and 
delegates interact according to the docs.

Foreach on an aggregate will use the opApply call (assuming ranges aren't being 
used).  So if we use a simple example below, what exactly is the delegate that 
is passed to opApply?  The docs say a delegate is a pairing of an object 
reference and a function, where the object is passed as the 'this' parameter to 
the function.  But that doesn't seem to be the case here.

Is a virtual object with a function encompassing the body of the foreach being 
silently created and passed in?

Thanks,
Jerry


class C {
  uint[] a;
  int opApply(int delegate(ref uint) dg) {
    int result = 0;
    for (size_t i=0; i < a.length; i++) {
      result = dg(a[i]);
      if (result) break;
    }
    return result;
  }
}
void foo() {
  C c = new C;
  foreach (uint v; c) { writefln(v); }
}

Reply via email to