In a Fortran module I have a function that takes an array and its name, gets from a database (actually calling a C function) the shape of the array, copies the array in a temporary buffer and passes the buffer to another C function that processes it. This Fortran function has the name fs_WriteData_i for integer data, fs_WriteData_f for real and fs_WriteData_d for double precision. All these functions accept not only one-dimensional arrays, but also 2D, 3D and 4D arrays and they work perfectly. Here follows the interface of one of these subroutines:
subroutine fs_WriteData_d(fieldname, data)
use, intrinsic :: iso_c_binding
implicit none
real(c_double), dimension(*) :: data
character :: fieldname*(*)
! ...
end subroutine fs_WriteData_d
If the user calls fs_WriteData_d('name', data)
with data being a double precision, up to 4-dimensional array, this subroutine does the job.
Now, to the question: I want to provide a common overloaded interface called fs_WriteData, so I use
interface fs_WriteData
module procedure fs_WriteData_i, &
fs_WriteData_f, &
fs_WriteData_d
end interface fs_WriteData
Unfortunately, this does not work: the compiler states that it cannot find the correct implementation if the user just calls fs_WriteData('name', data)
, and this because of a rank mismatch with all of these functions.
Is there a clever way to avoid writing all the fs_WriteData_d_1d, fs_WriteData_d_2d, ... subroutines with just the same content in order to make the module more maintainable?
Many thanks in advance.