指定子程序接口的模块

问题描述 投票:0回答:1

我的图书馆有一些模块,用于为某些子例程指定抽象接口,如下所示:

module abstract_module
    implicit none

    ! interface of subroutines
    abstract interface
        subroutine sub_interface(...)
           ...
        end subroutine
    end interface

end module

现在,在我的程序中,我编写了一个子例程,为了正确使用它,我必须对其进行声明,并且它可以正常工作

program testsub
    use abstract_module
    ...
    implicit none

    ! custom interface
    procedure(sub_interface) :: custom

    ! call the subroutine via another one
    call test_call(custom)
end program

现在,我想将所有自定义子例程收集到一个模块中,但是我不知道如何指定一个子例程实际上是一个接口:

module custom_subs
    use abstract_module
    implicit none

    ! this does not compile, obviously
    abstract interface
        procedure(sub_interface) :: custom
    end interface

contains

subroutine custom(...)
   ...
end subroutine

end module

有没有像我在程序中那样指定在模块中子例程的方法,还是必须将它们留在程序本身中?

module fortran fortran90
1个回答
0
投票

您有很多外部过程,并且在使用它们时希望为其提供显式接口。您正在考虑两种方法:

  • 使用接口块
  • 使程序本身成为模块程序

这是两种不同的和不兼容方法。您必须选择其中一个。您不能将它们混合使用同一步骤。

将外部过程转换为模块过程是值得称赞的目标。考虑模块

module custom_subs
  implicit none

contains

  subroutine custom(...)
     ...
  end subroutine

end module

也就是说,只需将过程放入模块中:不必担心使用此其他abstract_module模块或添加接口块。确实,销毁abstract_module-您不再需要custom作为外部过程,因此无法解决与它的接口。

此答案旨在解决有关方法是否互补的困惑。有关每种方法的更多详细信息,请参见this onethis one等其他问题。

© www.soinside.com 2019 - 2024. All rights reserved.