BCS Wrote:

> Hello Steve,
> 
> > foreach/opApply
> > 
> > Would it be a) true, and b) helpful if the documentation said
> > something like:
> > 
> > The body of the apply function iterates over the elements it
> > aggregates, passing them each to the dg function, 
> 
> > an implementation of
> > which is provided by the compiler for each opApply overload it
> > encounters.
> 
> I'm not shure that bit is correct. I'm not shure what you are saying.
> 
> > If the dg returns 0, then foreach goes on to the next
> > element. If the dg returns a nonzero value, as it will if, for
> > example, a break or goto statement is executed in the loop, then apply
> > must cease iterating and return that value. Otherwise, after iterating
> > across all the elements, apply will return 0.
> > 
> > The class need not contain an aggregate. The values iterated can be
> > calculated in opApply from other class members, though there should be
> > a corresponding class member  because of the ref in dg. The following
> > example should make the operation of foreach/opApply clear:
> > 
> > import std.stdio;
> > 
> > class Foo
> > {
> > uint orig;
> > uint cur;
> > this(uint n) { orig = n; cur = n; }
> > 
> > int opApply(int delegate(ref uint) dg)
> > {
> > writefln("enter opApply");
> > int result = 0;
> > for (int i = 0; i < 50; i++)
> > {
> > result = dg(cur);
> > writefln("Result %d", result);
> > if (result)
> > {
> > writefln(i);
> > cur = orig;
> > break;
> > }
> > cur += cur*3;
> > }
> > writefln("leave opApply");
> > return result;
> > }
> > }
> > void main()
> > {
> > Foo foo = new Foo(3);
> > foreach(uint u; foo)
> > {
> > writefln(u);
> > if (u > 200) goto L1;
> > }
> > L1:
> > foreach(uint u; foo)
> > {
> > writefln(u);
> > if (u > 10000) break;
> > }
> > foreach(uint u; foo)
> > {
> > writefln(u);
> > if (u > 10000) break;
> > // The delegate takes a ref uint
> > u = 0;
> > writefln(u);
> > }
> > }
> 
> 

Well, I'm not sure either, that's why I was asking. But I don't define the 
delegate, so I presume it must be done for me (as in how many C++ programmers 
does it take to change a light bulb).

Otherwise what I'm saying seems to fit my test program.

I'm just saying the documentation could be made more straightforward for 
beginners to use.

Reply via email to