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

Timoses timosesu at gmail.com
Sun Jul 8 19:22:32 UTC 2018


On Sunday, 8 July 2018 at 18:46:31 UTC, vino.B wrote:
> Hi All,
>
>  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.
>
> void process(T)(T function(string, int) coRoutine, string Test, 
> int Size) {

This would templetize the return type of the coRoutine, thus 
within that function you could do

     T returnedValue = coRoutine(string.init, int.init);

> alias scRType = typeof(coRoutine(string.init, int.init));
>
> Eg:
>
> Run1 : process(&Testfun, Test, Size);
> void process(T ...)(T function(string, int) coRoutine, string 
> Test) {
> alias scRType = typeof(coRoutine(string.init, int.init));
>
>
> Run2 : process(&Testfun, Test, Size, Str1);
> void process(T)(T function(string, int, string) coRoutine, 
> string Test, int Size, string Str1) {
> alias scRType = typeof(coRoutine(string.init, int.init, 
> string.int));
>
>
> Run3 : process(&Testfun, Test);
> void process(T)(T function(string, string) coRoutine, string 
> Test, int Size) {
> alias scRType = typeof(coRoutine(string.init));
> PFresult.get = coRoutine(args);
>
> Some what like this
>
> auto Testfun (string FFs, int Size) { return tuple(FFs, Size); }
>
> void process(T ...)(T function(T args) coRoutine, T args) {

This would mean that if you pass a function that returns for 
example an int, it must also
- take an int as argument
- and process has to accept another int
e.g.
     process((int i) => i+1, 3);

if T would be (int, string) you would have to pass something like

     process((int i, string s) {
         return AliasSeq!(int, string); }, // error: not even sure 
how to express this
             3, "hello");

where I'm not sure how to express the return type of (int, 
string)... Does anybody know this? Would the Tuple Dip make this 
possible? 
(https://forum.dlang.org/post/p3bdp1$2b4e$1@digitalmars.com)

> alias scRType = typeof(coRoutine(T.init));
> PFresult.get = coRoutine(T);
>
> void main() {
> string Test ="C:\\Temp\\BACKUP1"; int Size = 1;
> process(&Testfun, Test, Size);
> }
>
>
> Code : Working
> import std.stdio: writeln;
> import std.container.array;
> import std.typecons: tuple;
> import std.parallelism: taskPool;
>
> auto Testfun (string FFs, int Size) { return tuple(FFs, Size); }
>
> void process(T)(T function(string, int) coRoutine, string Test, 
> int Size) {
> alias scRType = typeof(coRoutine(string.init, int.init));
> auto PFresult = taskPool.workerLocalStorage!scRType();
> PFresult.get = coRoutine(Test, Size);
> foreach(i; PFresult.toRange) { writeln(i[][]); }
> }
>
> void main() {
> string Test ="C:\\Temp\\BACKUP1"; int Size = 1;
> process(&Testfun, Test, Size);
> }
>
> From,
> Vino.B

I suggest taking a look at 
https://dlang.org/phobos/std_traits.html .

E.g. ReturnType and Parameters:

     int func() { return 3; }
     assert(is(ReturnType!func == int));

     void gunc(int i, double j) {}
     import std.meta : AliasSeq;
     assert(is(Parameters!gunc == AliasSeq!(int, double)));


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?



More information about the Digitalmars-d-learn mailing list