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 15:49:50 UTC 2018


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"...


More information about the Digitalmars-d-learn mailing list