Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

storing a procedure pointer within a type in Fortran

I would like to store a pointer to a function as an attribute w/in a type. But the compiler complains that the first argument to the function ought to be the type (like the self or this argument in languages like Python or C++). But the attribute is just a pointer – it is not part of the type.

Here is some code:

module my_mod

  implicit none
  public :: my_type

  abstract interface
    ! INTERFACE I WANT TO POINT TO
    subroutine my_interface(arg)
      real, intent(in) :: arg
    end subroutine my_interface
  end interface

  type :: my_type
    ! MY TYPE DEFINITION
    real :: my_variable
    procedure(my_interface), pointer :: my_fn_pointer
  end type my_type

  interface my_type
    ! "CONSTRUCTOR" INTERFACE FOR MY TYPE
    module procedure init_my_type
  end interface

  contains

  type(my_type) function init_my_type(arg1, arg2)
    ! ACTUAL "CONSTRUCTOR" FOR MY TYPE
    real, intent(in) :: arg1
    procedure(my_interface) :: arg2

    init_my_type%my_variable = arg1
    init_my_type%my_fn_pointer => arg2
  end function init_my_type

end module my_mod

The actual error I get (ifort compiler) is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

error #8262: For a type-bound procedure that has the PASS binding attribute, the first dummy argument must have the same declared type as the type being defined.

I don’t understand why it thinks that the interface I want to point to is a type-bound procedure.

Any hints?

>Solution :

You are, reasonably, misled by the wording of the error message as it relates to type-bound procedures. This is a problem of the error message itself which is unclear.

Your derived type has no binding.

However, the "passed-object dummy argument" concept applies to a procedure pointer component (which you do have) just as much as it does to type-bound procedures.

You should use the NOPASS attribute when using procedure pointer components and type-bound procedures if you want to avoid the passed-object dummy.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading