How to use a user-defined-type in a Fortran interface

前端 未结 1 826
无人及你
无人及你 2020-12-11 20:11

In a Fortran 2003 module I\'m defining a type called t_savepoint and, later, I want to define an interface for a subroutine called fs_initializesavepoint<

相关标签:
1条回答
  • 2020-12-11 21:02

    Rightly or wrongly in an interface block you don't have access to the environment by host association. To fix this you need to import the datatype exlicitly:

    [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 the way you have put this suggests you are not going about coding this up the best way. Better is simply to put the routine itself in the module. Then you don't need bother with 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 modules are really designed to deal with connected entities this is really the way to do it in Fortran. It also has the advantage of only requiring a f95 compiler, so is universally available (though admittedly import is commonly implemented)

    0 讨论(0)
提交回复
热议问题