Passing function(whose parameter would be dynamic and the type is unknown) as a parameter to another function.

Alex sascha.orlov at gmail.com
Mon Jul 9 18:07:49 UTC 2018


On Monday, 9 July 2018 at 17:26:30 UTC, vino.B wrote:
>
> Request Help:
> void process(alias coRoutine, T...)(Array!string Dirlst, T 
> params)
> {
>       ReturnType!coRoutine rData;   ///// This line is not 
> working
>       alias scRType = typeof(coRoutine(string.init, T.init));
>       auto PFresult = taskPool.workerLocalStorage!scRType();
>       foreach (string FFs; parallel(Dirlst[],1)) { PFresult.get 
> ~= coRoutine(FFs, params); }
>       foreach(i; PFresult.toRange) { rData ~= i[][]; }
> }
>
> Error:
> test.d(206): Error: template instance 
> `std.traits.ReturnType!(coAgedDirClean)` does not match 
> template declaration ReturnType(func...) if (func.length == 1 
> && isCallable!func)

Yeah... for ReturnType to work, you need a function, but you have 
only a template.

The easy solution is to execute the template and to ask the 
result for its type:

´´´
void main()
{
	process!fun();
}

void process(alias coRoutine, T...)(T params)
{
	auto res = coRoutine(params);
	pragma(msg, typeof(res));
}

auto fun(T...)(T params)
{
	return 42;
}
´´´

If you need it in advance... It is a little bit longer. There was 
a place, where I used this once...

See
https://run.dlang.io/is/Xy6Xf4

However, I wonder why you need this, especially as your process 
is void. Why not just using auto for results of the coroutines?


More information about the Digitalmars-d-learn mailing list