On Wed, Jun 12, 2019 at 07:46:12PM +0000, Mek101 via Digitalmars-d-learn wrote:
[...]
> > public size_t indexOf(alias pred = "a == b", Range)(Range array)
> > {
> >     alias predicate = unaryFun!pred;
> >     for(size_t i = 0; i < array.length; i++)
> >             if(predicate(array[i]))
> >                     return i;
> >     return size_t.max;
> > }
> 
> Say that I may want to use the function in a @nogc scope, providing a
> @nogc callable, but I also want to reuse the same code in managed
> scope while passing a delegate: how can I apply the attributes of the
> given callable to the function so that they're automatically
> determined at compile time?
[...]

You don't need to do anything special; indexOf, as you declared it, is
already a template function, so the compiler should automatically apply
attribute inference to it. So if pred is @nogc, and the implementation
of indexOf itself doesn't invoke the GC, the compiler should
automatically infer @nogc for you.

One safeguard that you might want to consider is to write a @nogc
unittest, something like this:

        @nogc unittest
        {
                ...
                auto result = indexOf!(...)(...);
                ...
        }

The @nogc annotation on the unittest ensures that as long as the pred
argument to indexOf is @nogc, indexOf itself will also be @nogc. This
prevents future changes from accidentally introducing GC dependent code
in the implementation of indexOf, while at the same time not explicitly
marking indexOf as @nogc (only the unittest is annotated, not the
function itself) allows you to use it with GC-dependent predicates as
well.


T

-- 
Which is worse: ignorance or apathy? Who knows? Who cares? -- Erich Schubert

Reply via email to