Re: How to "Inherit" the attributes from a given callable argument?

2019-06-13 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, June 13, 2019 3:49:04 AM MDT Jacob Carlborg via Digitalmars-d-
learn wrote:
> On 2019-06-12 22:42, Mek101 wrote:
> > I didn't know it applied to templates other than lambdas.
> >
> > Thank you for your explanation.
>
> It applies to templates, lambdas (which basically are templates) and
> nested functions.

It also now applies to auto return functions, though that's a more recent
change.

- Jonathan M Davis





Re: How to "Inherit" the attributes from a given callable argument?

2019-06-13 Thread Jacob Carlborg via Digitalmars-d-learn

On 2019-06-12 22:42, Mek101 wrote:

I didn't know it applied to templates other than lambdas.

Thank you for your explanation.


It applies to templates, lambdas (which basically are templates) and 
nested functions.


--
/Jacob Carlborg


Re: How to "Inherit" the attributes from a given callable argument?

2019-06-12 Thread Mek101 via Digitalmars-d-learn

I didn't know it applied to templates other than lambdas.

Thank you for your explanation.


Re: How to "Inherit" the attributes from a given callable argument?

2019-06-12 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Jun 12, 2019 at 07:46:12PM +, 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