Question: on passing keyword argument from one function to another

I have function that accept keyword argument. From this function, I need to pass this argument to another function using the same signature and using same name for the keyword. But it is not possible to do. 

Easier to explain with an example.   This is what I want to do

restart;
foo:=proc(n::integer,{ic::`=`:=[]})   
   post_process(n+1,'ic'=ic);
end proc;

post_process:=proc(n::integer,{ic::`=`:= []})
    print("in postprocess, n=",n, "ic=",ic);
end proc;

foo(3,'ic'=(y(0)=5))

Gives 

The problem is that when calling post_process(), using 'ic'=ic it does not work. Currently I have to change the keyword name as workaround, like this

restart;
foo:=proc(A::integer,{ic::`=`:=[]})
   print("in foo, A=",A, "ic=",ic);
   post_process(A,'new_ic'=ic);
end proc;

post_process:=proc(A::integer,{new_ic::`=`:= []})
    print("in postprocess, A=",A, "ic=",new_ic);
end proc;

foo(3,'ic'=(y(0)=5))

Which gives

Now it passed it. I would have liked to keep same keyword name ic in both function signatures instead of coming up with new name.

Is there a way to do that? 

This is not the only problem. The problem with this workaround, is that it does not work when the keyword argument is not passed to the first function. Because now it will use the default values of [].

Hence when calling the post_process() function, it fails due to type mismatch. This now gives an error

restart;
foo:=proc(A::integer,{ic::`=`:=[]})
   print("in foo, A=",A, "ic=",ic);
   post_process(A,'new_ic'=ic);
end proc;

post_process:=proc(A::integer,{new_ic::`=`:= []})
    print("in postprocess, A=",A, "ic=",new_ic);
end proc;

foo(3)

 

Error, (in foo) invalid input: post_process expects value for keyword parameter new_ic to be of type `=`, but received []

So the way I do this now, is to add an extra check each time, like this

restart;
foo:=proc(A::integer,{ic::`=`:=[]})
   if nargs = 2 then #is ic passed in?     
      post_process(A,'new_ic'=ic);
   else
      post_process(A);
   fi;
end proc;

post_process:=proc(A::integer,{new_ic::`=`:= []})
    print("in postprocess, A=",A, "ic=",new_ic);
end proc;

foo(3)# now it works

But in my actual code, I have more than one keyword argument, and I have to keep checking for correct number of arguments each time, to know which arggument to pass along. 

So far, I did not find an easy way around this.

Any suggestion how to do this better? passing keyword argument with default values? from one function to another without getting into these problems?

Maple 2020.2

 

Please Wait...