Search code examples
fortranproceduregeneric-programmingfortran2003

Fortran generic procedure error: may not be generic


I know that you can make generic procedures using abstract types, like in here:

fortran class declaration of dummy argument

But can I do the same sort of thing with the following code?

module proc_mod
  public :: forced,ideal
  interface forced;    module procedure forced1;    end interface
  interface forced;    module procedure forced2;    end interface
contains

function forced1() result(forced)
 implicit none
 integer:: forced
 forced = 1
end function

function forced2(t) result(forced)
 implicit none
 integer,intent(in) :: t
 integer:: forced
 forced = t
end function

function ideal() result(i)
 implicit none
 integer:: i
 i = 2
end function

end module

program procTest
  use proc_mod
  implicit none

  integer :: n

  procedure(forced), pointer:: funPointer => NULL()

  write(*,'(A)') "Please enter the type of vortex calculation you wish to use."
  read(*,*) n                                                                  

  select case( n )
    case( 1 );    funPointer => forced
    case( 2 );    funPointer => ideal
    case default; funPointer => ideal
  end select

  write(*,'(A,I3)') "You chose function: ", funPointer()

  stop
end program

Right now I'm getting the error: funPointer may not be generic. I'm sure there's a way around it, but I'm unfamiliar with generic procedures.


Solution

  • No, you cannot make procedure pointer to a generic procedure. You also cannot pass generic procedures as dummy argument. You must always choose one specific function.

    You should perhaps explain what is your intention, not just show some code and ask if it can be done differently. But anyway, take in mind that Fortran generics are used for compile time dispatch.