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

Timoses timosesu at gmail.com
Mon Jul 9 07:44:18 UTC 2018


On Monday, 9 July 2018 at 05:54:27 UTC, vino.B wrote:
> On Sunday, 8 July 2018 at 19:22:32 UTC, Timoses wrote:
>> Perhaps you could tell us what your goal is. People here might 
>> come up with a nice solution. Why do you feel like having to 
>> use templated functions in the first place? That is, what is 
>> the generic goal of the functions you are trying to define?
>
> Hi Timoses,
>
>  We are converting a Power shell script to D in phased manner; 
> the PS script has many functions and we converted few function 
> to D in Phase 1.
>
> Phase 1:
>
> Structure of the Program
>   Main -> Thread Manager->CoFunction1(Fs1,2,3,4,5)
>   Main -> Thread Manager->CoFunction2(Fs1,2,3,4,5)
>
>
> The thread manager will call the Cofunctions and the function 
> gets executed on “N” of file systems each of size 5-10 TB.
>
> The function that we transformed all has the same number of 
> parameters (3) and the type was same (string, string, int), so 
> we wrote a static thread manager as below
>
> void ptManager (T)(T function(string, string, int) coRoutine, 
> Array!string Dirlst, string Step, int Size) {
>      alias scRType = typeof(coRoutine(string.init, string.init, 
> int.init));
>      auto PFresult = taskPool.workerLocalStorage!scRType();
>      ReturnType!coRoutine rData;
>     foreach (string FFs; parallel(Dirlst[0 .. $],1)) { 
> PFresult.get ~= coRoutine(FFs.strip, Step); }
>      foreach(i; PFresult.toRange) { writeln(i[][]); }
> }
>
> void main () {
> ptManager(&function1, Fn1Dirlst, Step, Size);
> ptManager(&function2, Fn2Dirlst, Step, Age);
>
> }
>
> Phase 2:
>
> In phase 2 we are transferring few more function to the 
> existing D code, and these functions has variable number of 
> parameter and different type eg: Function3(string, string, 
> string), Function(string, int, string, int).
>
> Initially I tried to re-write the ptManager function for each 
> type of function which ended with 8 ptManager functions, Eg : 
> ptManager1(string, string, int), ptManager2(string, string, 
> string), ptManager3(string,int), so now trying as to whether we 
> can use 1 ptManager function which can process function with 
> “N” of parameter and types to process all the function, hence 
> trying to implement the Variadic function. Hence request your 
> help and suggestion to achieve this.
>
> Command Parameter to all the functions:
> Array!string Dirlist : This parameter will contain the the Fs 
> names
> File logF: This parameter is used to store the outoput
> File logE: This parameter is used to store the Error 
> Information.
>
> Rest of the function has variable parameters/type.
>
> From,
> Vino.B

Ok, interesting. I don't really know my way around parallelism, 
though I think you should take a look at Alex's suggestion 
(https://forum.dlang.org/post/adepbwetsiuwguuaaizs@forum.dlang.org) for a template solution. You could e.g. do

     void ptManager(alias func, T ...)(T args)
     {
         import std.traits : Parameters, ReturnType;
         static assert(is(Parameters!func == T), "Arguments don't 
fit function!");
         alias RetType = ReturnType!func;

         // only receive value if return type is not void
         static if (!is(RetType == void))
             RetType mVal = func(args);

         // write it to stdout if mVal is a valid symbol
         // (could also combine that in the above static if...)
         static if (is(typeof(mVal)))
         {
             import std.stdio : writeln;
             writeln(mVal);
         }
     }

     unittest
     {
         ptManager!((int i) => i+1)(2);
         void someDelegate(string s1, string s2, int i1) { /* ... 
*/ }
         ptManager!someDelegate("hello", "ptManager", 100);
     }


More information about the Digitalmars-d-learn mailing list