我的图书馆有一些模块,用于为某些子例程指定抽象接口,如下所示:
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 custom_subs
implicit none
contains
subroutine custom(...)
...
end subroutine
end module
也就是说,只需将过程放入模块中:不必担心使用此其他abstract_module
模块或添加接口块。确实,销毁abstract_module
-您不再需要custom
作为外部过程,因此无法解决与它的接口。