Optional arguments in Fortran, when the signature of the function call is not initially known

92 views Asked by At

I have a fortran subroutine that takes many optional arguments and I am trying to build a python interface for it

subroutine myfunc(mandatory, A, B, C, result)
   integer, intent(in) :: mandatory
   integer, optional, intent(in) :: A, B, C
   integer, intent(out) :: result
end subroutine myfunc

In the python function call the mandatory arguments are given, while the optional arguments are given as key/value pairs in a dictionary. The fortran function that interfaces with python takes the arguments of the python call and checks if particular keys exists and stores it in boolean variables.

The python call would looks something like this

result = pymyfunc(1, {'A': 1, 'C': 2})

And the interface function will hold boolean values

is_A = .true.
is_B = .false.
is_C = .true.

Then it is about building the logic of all possible combinations of optional values in an if/else block to appropriatelly call the fortran routine. In this case:

...
else if (is_A .and. .not. is_B .and. is_C) then

   call myfunc(mandatory,A=A,C=C,result)
...

Now, this if/else block is manageable for 3 optional arguments. But you can see how this could explode in cases where more optional arguments exist.

I wonder if there is a more intelligent way of solving this. Something that I might be missing on how fortran handles the function calling signatures.

0

There are 0 answers