Passing function(whose parameter would be dynamic and the type is unknown) as a parameter to another function.
vino.B
bheeman.vino at hotmail.com
Mon Jul 9 17:26:30 UTC 2018
On Monday, 9 July 2018 at 15:49:50 UTC, Alex wrote:
> On Monday, 9 July 2018 at 15:40:53 UTC, vino.B wrote:
>> On Sunday, 8 July 2018 at 19:10:24 UTC, Alex wrote:
>>> On Sunday, 8 July 2018 at 18:46:31 UTC, vino.B wrote:
>>>> Request you help, in the below code we pass the function
>>>> "Testfun" as a parameter to another function "process" in
>>>> order for the function "process" to work we have to specify
>>>> the type of the parameter that is passed to the function "(T
>>>> function(string, int) coRoutine, string Test, int Size) ",
>>>> so now how do we pass a function whose parameter would be
>>>> dynamic and the type is unknown.
>>>
>>> What do you mean with a "function with dynamic parameters"
>>> and "unknown type"?
>>>
>>> But how about
>>>
>>> ´´´
>>> void main()
>>> {
>>> process!fun();
>>> }
>>>
>>> void process(alias coRoutine, T...)(T params)
>>> {
>>> coRoutine(params);
>>> }
>>>
>>> auto fun(T...)(T params)
>>> {
>>>
>>> }
>>> ´´´
>>
>> HI Alex,
>>
>> I tried you method, but it not working as expected, the
>> process!fun1(Test1) works but the process!fun2(Test1, Size )
>> does not work, instead of displaying the value (Test, 1) it
>> output's the parameter "Test1".
>>
>> Output:
>> Test
>> Test1
>>
>> If I comment process!fun1(Test1); then the output is same
>>
>> Output :
>> Test1
>>
>> import std.stdio: writeln;
>>
>>
>> void main()
>> {
>> string Test1 = "Test";
>> int Size = 1;
>> process!fun1(Test1);
>> process!fun2(Test1, Size );
>> }
>>
>> void process(alias coRoutine, T...)(T params)
>> {
>> coRoutine(params);
>> }
>>
>> auto fun1(T...)(T params)
>> {
>> writeln(params);
>> }
>>
>> auto fun2(T...)(T params)
>> {
>> writeln(params);
>> }
>>
>> From,
>> Vino.B
>
> As I can see, this is the expected output.
> The "1" after "Test" is the value of parameter "Size"...
Hi Alex,
Thank you a lot, i was able to improvise using your logic, and i
need more help, the below code is working as expected using your
logic, now how do i get the return type of the function
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)
Working Code:
auto coAgedDirClean(T...)(string FFs, T params) {
auto dFiles = Array!(Tuple!(string,
SysTime))(dirEntries(FFs, SpanMode.shallow).filter!(a => a.isFile
&& a.size > params[0]).map!(a => tuple(a.name,
a.timeLastModified)));
return dFiles;
}
auto coAgedDirDelete(T...)(string FFs, T params) {
auto dFiles = Array!(Tuple!(string,
SysTime))(dirEntries(FFs, SpanMode.shallow).filter!(a => a.isFile
&& a.size > params[0]).map!(a => tuple(a.name,
a.timeLastModified)));
if (params[1] == "run" && !dFiles.empty) { foreach(d;
parallel(dFiles[], 1)) { remove(d[0]); }}
return dFiles;
}
void process(alias coRoutine, T...)(Array!string Dirlst, T params)
{
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) { writeln(i[][]); }
}
void main()
{
Array!string AgedDir, DeleteDir;
AgedDir.insert("C:\\Temp\\TEAM1");
DeleteDir.insert("C:\\Temp\\BACKUP1");
ulong Size = 102;
string Step = "run";
process!coAgedDirClean(AgedDir, Size );
process!coAgedDirDelete(DeleteDir, Size, Step);
}
From,
Vino.B
More information about the Digitalmars-d-learn
mailing list