digitalmars.D.learn - How to "Inherit" the attributes from a given callable argument?
- Mek101 (11/19) Jun 12 2019 Say that I may want to use the function in a @nogc scope,
- H. S. Teoh (26/40) Jun 12 2019 [...]
- Mek101 (2/2) Jun 12 2019 I didn't know it applied to templates other than lambdas.
- Jacob Carlborg (5/8) Jun 13 2019 It applies to templates, lambdas (which basically are templates) and
- Jonathan M Davis (5/11) Jun 13 2019 It also now applies to auto return functions, though that's a more recen...
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?
Jun 12 2019
On Wed, Jun 12, 2019 at 07:46:12PM +0000, Mek101 via Digitalmars-d-learn wrote: [...][...] 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 Schubertpublic 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?
Jun 12 2019
I didn't know it applied to templates other than lambdas. Thank you for your explanation.
Jun 12 2019
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
Jun 13 2019
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:It also now applies to auto return functions, though that's a more recent change. - Jonathan M DavisI 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.
Jun 13 2019