I know it’s non-standard pass a scalar variable into a procedure argument where an array is expected, as discussed in other threads on this site. (But it is acceptable to do the reverse, to pass a single array element into a scalar argument.)
This limitation requires developers to make a special overload procedure (generic interface) to handle the scalar case. Here’s an example of what I had to do in some legacy code when I recently updated compiler versions from gfortran 7 to 10+, because some non-standard code that was previously compiling (either unchecked or by extension) now triggers a compile error for this very reason.
call inread(iunit, 5, array_variable) ! read 5 values and store in array elements 1-5
call inread(iunit, 1, array_variable(6)) ! read 1 value and store in array element 6 by sequence association
call inread(iunit, 1, scalar_variable) ! ERROR: rank mismatch with gcc 10+
Here’s a representation of inread’s interface, where val is an array:
subroutine inread_array(iunit,nvars,defin,val)
implicit none
integer iunit
integer nvars
character*(*) defin(nvars)
double precision val(nvars)
...
So I had to make a generic interface just to handle the scalar case by just copying the scalar in and out of a 1-element array:
interface inread
module procedure inread_array
module procedure inread_scalar
end interface inread
contains
subroutine inread_array(iunit,nvars,defin,val)
...
end subroutine inread_array
subroutine inread_scalar(iunit,nvars,defin,valout)
implicit none
integer iunit
integer nvars ! must be 1
character*(*) defin(nvars)
double precision val(nvars), valout
call inread_array(iunit,nvars,defin,val)
valout = val(1) ! reassign the array element to the scalar output variable
end subroutine inread_scalar
Would it be possible with explicit interfaces for the compiler to simply treat a scalar actual argument as an assumed-size array of size 1 and the proper rank to match the dummy argument?