const version for foreach/opApply

Matthias Walter xammy at xammy.homelinux.net
Fri Jun 8 09:33:20 PDT 2012


Hi,

trying to traverse the entries of a std.bitmanip.BitArray I stumbled
upon the following problem:

The original code is as follows:

int opApply(scope int delegate(ref bool) dg)
{
  int result;
  for (size_t i = 0; i < len; i++)
  {
    bool b = opIndex(i);
    result = dg(b);
    this[i] = b;
    if (result)
      break;
  }
  return result;
}

In case I want to accept const(BitArray) objects, it shall look like the
following (maybe using "ref const(bool)" for the delegate parameter?):

int opApply(scope int delegate(bool) dg) const
{
  int result;
  for (size_t i = 0; i < len; i++)
  {
    bool b = opIndex(i);
    result = dg(b);
    if (result)
      break;
  }
  return result;
}

Can one glue both things together into a single routine (using inout
magic or whatever)?

Best regards,

Matthias Walter


More information about the Digitalmars-d-learn mailing list