|
Archives
D Programming
digitalmars.Ddigitalmars.D.bugs digitalmars.D.dtl digitalmars.D.ide digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger D.gnu D C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript electronics |
digitalmars.D - question about foreach, opApply, and delegates
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); }
}
Jun 08 2009
Jerry Quinn wrote: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? Jun 08 2009
Hello grauzone,Jerry Quinn wrote: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? Jun 08 2009
On Mon, Jun 8, 2009 at 8:55 AM, Jerry Quinn<jlquinn optonline.net> wrote:Hi, all. =A0I find myself a little confused about how foreach, opApply, a= Jun 08 2009
Jerry Quinn wrote: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? Jun 08 2009
|