0
votes

I am trying to pass a procedure pointer to a derived-type-bound-procedure

module mymodule
use mystoremodule
    implicit none
    private
    type, abstract, public :: mytype
    contains
        procedure :: parse
    end type mytype
contains

subroutine parse(self,mypointer)
    implicit none

    ! Declaring Part
    class(mytype) :: self
    procedure(storing),pointer, intent(in) :: mypointer 
    integer :: myvalue

    ! Executing Part
    myvalue = 42
    call mypointer(myvalue)

end subroutine parse
end module mymodule

where storing is defined in another module/ derived type

module mystoremodule
    implicit none
    type, public :: storingtype
    integer :: myvalue
    contains
        procedure, public :: storing
    end type storingtype
contains

subroutine storing(self,myvalue)
    ! Declaring part
    class(storingtype) :: self
    integer, intent(in) :: myvalue
    ! Executing part
    self%myvalue = myvalue
end subroutine  SetExcitationOrder
end module mystoremodule

I call the procedure by

call mytypeobject%parse(storingtypeobject%storing)

With that i get a compiler error

The type of the actual argument differs from the type of the dummy argument.

I found out that the error comes from the procedure pointer not passing the dummy argument to the storing procedure (i didn't define anything as nopass). In all other cases the dummy argument gets passed automatically, why not here? It's not feasible for me to declare the dummy argument, as the object which the procedure uses changes. Are there any solutions to my problem?

1
Can you show a complete program, or at least show all relevant declarations? - francescalus
Sorry, not possible as the code is confidential. Are you missing a specific part of the the declarations? - THo
We can probably assume type(mytype) mytypeobject and type(storingtype) storingtypeobject but confirming that would be good. - francescalus
This question may also be of interest. - francescalus

1 Answers

0
votes

Well you are not passing there a procedure pointer. storingtypeobject%storing is not a procedure pointer, it is a binding to a type-bound procedure, not a pointer of any kind.

I wouldn't actually accept a pointer, but just a procedure argument in parse.

Something like:

subroutine parse(self,mypointer)
    implicit none

    ! Declaring Part
    class(mytype) :: self
    procedure(storing), intent(in) :: myproc
    integer :: myvalue

    ! Executing Part
    myvalue = 42
    call myproc(myvalue)

end subroutine parse

and

    call mytypeobject%parse(storing_wrapper)

  contains

    subroutine storring_wrapper(myvalue)
      integer, intent(in) :: myvalue
      call storingtypeobject%storing(myvalue)
    end subroutine

I think procedure pointers are mostly useful only if it can be changed somewhere. Not necessarily in parse but if you need to set it to some other target in some situations.