On Thursday, 15 September 2016 at 18:23:14 UTC, Q. Schroll wrote:
Why does it do that?
And seemingly it does not require it for opApply with more than two arguments.

Here's what the comment says:


// opApply with >2 parameters. count the delegate args.
// only works if it is not templated (otherwise we cannot count the args)
    void each(Iterable)(Iterable r)
if (!isRangeIterable!Iterable && !isForeachIterable!Iterable && __traits(compiles, Parameters!(Parameters!(r.opApply))))
        auto dg(Parameters!(Parameters!(r.opApply)) params) {
            return 0; // tells opApply to continue iteration

Usually, if you change elements while iterating, you need to `ref` them. If you leave out `ref` the following loop will finish after 'd'. If you put in `ref` you have an infinite loop.

void main()
  import std.stdio : writefln;
  auto arr = ['a', 'b', 'c', 'd'];
  foreach (ref i, c; arr)
    if (c == 'c')
      writefln("index = %d", i);

Reply via email to