Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
June 12, 2019 How to "Inherit" the attributes from a given callable argument? | ||||
---|---|---|---|---|
| ||||
I'll try to be straight. I have the following function: >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? I know of std.traits.SetFunctionAttributes and std.traits.FunctionAttributes, but I have no idea on how to apply them to solve this. Perhaps I should declare indexOf as a template? |
June 12, 2019 Re: How to "Inherit" the attributes from a given callable argument? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mek101 | 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 |
June 12, 2019 Re: How to "Inherit" the attributes from a given callable argument? | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | I didn't know it applied to templates other than lambdas. Thank you for your explanation. |
June 13, 2019 Re: How to "Inherit" the attributes from a given callable argument? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mek101 | 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 |
June 13, 2019 Re: How to "Inherit" the attributes from a given callable argument? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | 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
|
Copyright © 1999-2021 by the D Language Foundation