Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading functions with Fortran

In Fortran 90, we can overload functions with an interface. However, according to this site, we cannot define these functions with the same arguments name. With gfortran, it does not seem to be a problem as the following code works well enough:

interface check
  module procedure check_int, check_real
end interface

contains 

subroutine check_int(cur, dname, func_name, fname)
  integer, allocatable, intent(in) :: cur(:)
  character(*) :: dname, func_name, fname
  ...
end subroutine

subroutine check_real(cur, dname, func_name, fname)
  real, allocatable, intent(in) :: cur(:)
  character(*) :: dname, func_name, fname
  ...
end subroutine

So, is it bad practice to do so?

Edit: Calling the function with keywords does not change anything.

like image 817
alex_reader Avatar asked Dec 05 '12 11:12

alex_reader


1 Answers

Your example is perfectly valid. They can be distinguished by the TYPE of the arguments. The names are not important then. In your case the type of the cur argument differs.

The arguments with the same name can be distinguished by their type, kind or rank (TKR compatibility).

The point of the referenced article is that you cannot distinguished two specific procedures only by the ORDER of the arguments. It is because the procedures can be called with the keyword arguments in any order. This can be overcomed by using different names for the arguments.

Otherwise declaring more specific procedures for a generic one with the same names of arguments, but with different types/kinds/ranks is very common and perfectly valid.

Fortran 2003/2008 adds some more possibilities to generic resolution. It is also possible to distinguish procedures by the pointer/allocatable attribute of their arguments and by the procedure pointer dummy arguments.

like image 83
Vladimir F Героям слава Avatar answered Oct 20 '22 01:10

Vladimir F Героям слава