Is there any way to automatically initialize a constant array of procedure pointers?
I have a bunch of routines, which have to be called depending on the value of an integer variable. Instead of using a select case
statement, I would like to use procedure pointers as given below. However, it would be nice, if I could skip the explicit initialization of the procedure pointer array, and just define it as a constant array of wrapped procedure pointers. The code below demonstrates the solution I've found, the commented lines indicate the goal, I'd 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