Skip to content

Arrays of procedure pointers #139

Open
@rjfarmer

Description

@rjfarmer

I propose adding the ability for procedure pointers to be arrays, thus making something like this valid:

procedure(afunc), pointer, dimension(:) :: funcptr

This would allow building up "lists" of functions to be created that can be passed to a function to "process", or to make it easier to call different functions based on some index value. This would also make procedure pointers similar to other objects (and pointers) like ints, floats, derived types etc which can be arrays.

Use cases:

allocate(funcptr(3))
null(funcptr)
funcptr(1) => function1
funcptr(2) => null()
funcptr(3) =>  function3

call some_routine(funcptr)
deallocate(funcptr)

subroutine some_routine(funcptr)
    do i=1,size(funcptr)
          if(associated(funcptr(i))) call funcptr(i)
    end do

Different use case (which i have in my code)

SELECT CASE (id)
case(1)
    ptr => func1
case(2)
    ptr => func2
.....

call ptr()

Which could be made cleaner with:

 ! Inside an init function
 funcptr(1) => function1
 funcptr(2) => function2   

 ! Calling code
 call funcptr(id)

Currently you can do something similar if you do

type mytype
     procedure(afunc), pointer :: ptr
end type mytype

type(mytype), allocatable, dimension(:) :: funcptr

But this just hides whats going on and is no-obvious to a user/(me) why the pointer needs to be put inside a derived type (when you can have real, pointer, dimension(:) ).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Clause 7Standard Clause 7: Types

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions