How to use a custom type in a Fortran interface

In the Fortran 2003 module, I define a type called t_savepoint , and later I want to define an interface for a routine called fs_initializesavepoint that takes an object of type t_savepoint as an argument.

Here is the code for the whole module:

 module m_serialization implicit none type :: t_savepoint integer :: savepoint_index real :: savepoint_value end type t_savepoint interface subroutine fs_initializesavepoint(savepoint) type(t_savepoint) :: savepoint end subroutine fs_initializesavepoint end interface end module m_serialization 

The reason I need such an interface is because later on I will make this fortran module interact with C.

If I try to compile it (gfortran-4.7.0), I get the following error message:

  type(t_savepoint) :: savepoint 1 Error: The type of 'savepoint' at (1) has not been declared within the interface 

The error disappears if I go on to type definition inside a routine; but if then I want to use the same type in many routines, should I repeat the definition in all of them?

Thanks in advance.

EDIT : The solution would be to move the type definition to another module, and then to use in each subroutine. However, I do not really like this solution, because the t_savepoint type and routines are part of the same conceptual theme.

+4
source share
2 answers

Right or wrong in the interface block, you do not have access to the environment through host associations. To fix this, you need to import the data type implicitly:

 [ luser@cromer stackoverflow]$ cat type.f90 module m_serialization implicit none type :: t_savepoint integer :: savepoint_index real :: savepoint_value end type t_savepoint interface subroutine fs_initializesavepoint(savepoint) Import :: t_savepoint type(t_savepoint) :: savepoint end subroutine fs_initializesavepoint end interface end module m_serialization [ luser@cromer stackoverflow]$ gfortran -c type.f90 

This is f2003.

However, I suspect that you thought it was not your intention to code it in the best way. It’s better to just put the procedure in the module. Then you don’t have to worry about the interface at all:

 module m_serialization implicit none type :: t_savepoint integer :: savepoint_index real :: savepoint_value end type t_savepoint Contains Subroutine fs_initializesavepoint(savepoint) type(t_savepoint) :: savepoint Write( *, * ) savepoint%savepoint_index, savepoint%savepoint_value End Subroutine fs_initializesavepoint end module m_serialization [ luser@cromer stackoverflow]$ gfortran -c type.f90 

Given that the modules are really designed to work with related objects, this is really a way to do this in Fortran. This also has the advantage that only the f95 compiler is required, so it is available everywhere (although admittedly, imports are usually implemented)

+7
source

The IMPORT statement can make a difference when defining an abstract data type / class. Depending on the data type defining the abstract interface, it may be required, and you need to use the IMPORT statement to access the derived type inside the abstract interface block.

-1
source

All Articles