www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to "Inherit" the attributes from a given callable argument?

reply Mek101 <anEmailAddress mail.somewhere> writes:
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
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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
Jun 12 2019
parent reply Mek101 <oidgemek101 gmail.com> writes:
I didn't know it applied to templates other than lambdas.

Thank you for your explanation.
Jun 12 2019
parent reply Jacob Carlborg <doob me.com> writes:
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
parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
Jun 13 2019