On 08/11/2018 12:00 PM, Alex wrote:
´´´
import std.experimental.all;

static assert(isIterable!S);

[...]

struct S
{
[...]
     int opApply(scope int delegate(ref uint) /*@nogc*/ operations) //@nogc
     {
[...]
     }
}
´´´

Everything works fine, before I try to use the opApply function inside a @nogc function.

If I do, compiler complains, that opApply is not marked as @nogc. Ok.
If I try to mark opApply @nogc, I would have to mark operations delegate also as @nogc, but I can't do this, as I do not know a priori, how it will be used.

Now, as I learned at some point, a possibility would be, to templatify the function, as the compiler can then derive, if @nogc apply or not.
But if I write
´´´
int opApply()(...){...}
´´´
Then the static assert from above refuses to compile.

So... how to solve this case?

You can provide to overloads: one with @nogc, one without it. To keep it somewhat DRY, you can let them forward to a template implementation:

    int opApply(scope int delegate(ref uint) operations)
    {
        return opApplyImpl(operations);
    }
    int opApply(scope int delegate(ref uint) @nogc operations) @nogc
    {
        return opApplyImpl(operations);
    }
    int opApplyImpl(O)(O operations)
    {
        /* ... implementation here ... */
    }

Reply via email to