Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constant function pointer array in Fortran 2003

Dear Fortran programmers,

does anybody know, whether it is possible to declare a constant (parameter) procedure pointer array in Fortran 2003 or above?

As given below, I have a switcher function, which calls different functions depending on an incoming integer argument. It uses an array of procedure pointers (wrapped in derived) types. This array has to be initialized via the init() routine during run time before it can be used. Is there any way, to initialize this array already during compilation and avoid the necessity of such an initialization routine? It could be then also defined as parameter, since it's value won't change during the run.

module testmod
  implicit none

  interface
    function funcInterface() result(res)
      integer :: res
    end function funcInterface
  end interface

  type :: ptrWrap
    procedure(funcInterface), nopass, pointer :: ptr
  end type ptrWrap

  type(ptrWrap) :: switcher(2)

contains

  subroutine init()
    switcher(1)%ptr => func1
    switcher(2)%ptr => func2
  end subroutine init

  function callFunc(ii) result(res)
    integer, intent(in) :: ii
    integer :: res
    res = switcher(ii)%ptr()
  end function callFunc

  function func1() result(res)
    integer :: res
    res = 1
  end function func1

  function func2() result(res)
    integer :: res
    res = 2
  end function func2

end module testmod


program test
  use testmod
  implicit none

  call init()  ! I'd like to get rid of this call.
  print *, callFunc(1)
  print *, callFunc(2)

end program test
like image 883
Bálint Aradi Avatar asked Sep 28 '22 14:09

Bálint Aradi


2 Answers

Fortran 2008 permits initialization of procedure pointers and procedure pointer components to procedure targets (versus NULL()). The syntax is consistent with other initializers.

! In the scope of the module.
...
type(ptrWrap), parameter :: switcher(2) = [ptrWrap(func1), ptrWrap(func2)]
...
like image 98
IanH Avatar answered Oct 07 '22 01:10

IanH


I don't think it is allowed. From my reading of the 2008 standard parameter is an attribute of a data object and the class of data objects does not include procedures or procedure pointers.

What did your compiler tell you when you fed it your code ?

like image 27
High Performance Mark Avatar answered Oct 06 '22 23:10

High Performance Mark