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

John Dougan jdougan at acm.org
Wed Apr 10 03:25:06 UTC 2024


Below is a example program that illustrates my issue.

When compiled at run.dlang I get:
```
onlineapp.d(18): Error: `@safe` function 
`onlineapp.processSafely!(1, 4).processSafely` cannot call 
`@system` function pointer `shouldDo`
onlineapp.d(28): Error: template instance 
`onlineapp.processSafely!(1, 4)` error instantiating
```

Why isn't this working? How can I get the effect I want?

Cheers,
-- john

```
bool[7] stagesToProcess = false;

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

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

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

     @nogc nothrow @safe uint processSafely(ShouldDoFnT shouldDo)
     {
         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