How can I tell D that function args are @nogc etc.

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Wed Apr 10 11:34:06 UTC 2024


Place your attributes on the right hand side of the function, not the 
left side.

Use the left side for attributes/type qualifiers that go on the return type.

```d
bool[7] stagesToProcess = false;

bool shouldDoInStages(int index) @nogc nothrow @safe
{
      return stagesToProcess[index];
}

bool shouldDoNever(int index) @nogc nothrow @safe
{
      return false;
}

template processSafely(int low, int high)
{
      alias ShouldDoFnT = bool function(int) @nogc nothrow @safe;

      uint processSafely(ShouldDoFnT shouldDo) @nogc nothrow @safe
      {
          assert(low < high);
          uint stepsProcessed;
          for (int ii = low; ii <= high; ii++)
          {
              if (shouldDo(ii))
              {
                  stepsProcessed++;
              }
          }
          return stepsProcessed;
      }
}

void main()
{
      stagesToProcess = [false, false, true, true, false, true,
false];
      uint count = processSafely!(1, 4)(&shouldDoInStages);
      assert(count == 2);
}
```


More information about the Digitalmars-d-learn mailing list