Is there a way to automatically initialize a constant array of procedure pointers?
I have a bunch of routines that need to be called depending on the value of an integer variable. Instead of using instructions, select case
I would like to use pointers to procedures, as shown below. However, it would be nice if I could skip the explicit initialization of the array of procedure pointers and just define it as a constant array of wrapped procedure pointers. The code below demonstrates the solution I found, the commented lines indicate the goal I would like to achieve:
module testmod
implicit none
abstract interface
subroutine subInterface()
end subroutine subInterface
end interface
type :: SubPtr
procedure(subInterface), nopass, pointer :: ptr
end type SubPtr
! Would be nice to use something like this:
!type(SubPtr), parameter :: subs(2) = [ SubPtr(sub1), SubPtr(sub2) ]
contains
subroutine sub1()
print *, "SUB1"
end subroutine sub1
subroutine sub2()
print *, "SUB2"
end subroutine sub2
end module testmod
program test
use testmod
implicit none
type(SubPtr) :: subs(2)
integer :: ii
! Would be nice to get rid of those two initialization lines
subs(1) = SubPtr(sub1)
subs(2) = SubPtr(sub2)
! Testing procedure pointer array
do ii = 1, 2
call subs(ii)%ptr()
end do
end program test