如下所示,父模块和各个子模块分别放在单独的文件里。子模块 D 能通过 use 关联调用子模块 C 里定义的过程吗?
(File) module root_mod
|
|-(File) submodule C
| public module procedure taskC
|
|-(File) submodule D
use submodule C ? or use root_mod:submodule C
call taskC
如下所示,父模块和各个子模块分别放在单独的文件里。子模块 D 能通过 use 关联调用子模块 C 里定义的过程吗?
(File) module root_mod
|
|-(File) submodule C
| public module procedure taskC
|
|-(File) submodule D
use submodule C ? or use root_mod:submodule C
call taskC
Each parent module and submodule lives in a separate file, as illustrated here. Is use association a way for submodule D to invoke a procedure from submodule C?
(File) module root_mod
|
|—(File) submodule C
| public module procedure taskC
|
|—(File) submodule D
use submodule C ? or use root_mod:submodule C
call taskC
做两处修改就能互相调用。
把子过程的接口放进父模块
不需要 use,因为所有子模块都属于同一个父模块
完整的可运行例子如下。
demo.f90:
module root_mod
implicit none
interface
module subroutine routine_c()
end subroutine routine_c
module subroutine routine_d()
end subroutine routine_d
end interface
end module root_mod
program demo
use root_mod
implicit none
call routine_d()
end program demo
c.f90:
submodule(root_mod) c
implicit none
contains
module subroutine routine_c
print *, "hello from routine_c"
end subroutine routine_c
end submodule c
d.f90:
submodule(root_mod) d
implicit none
contains
module subroutine routine_d
print *, "hello from routine_d"
call routine_c()
end subroutine routine_d
end submodule d
They can invoke one another after two adjustments:
Put interfaces for the child routines into the parent module
You don't need use, as every child shares that parent module
A full working example follows:
demo.f90:
module root_mod
implicit none
interface
module subroutine routine_c()
end subroutine routine_c
module subroutine routine_d()
end subroutine routine_d
end interface
end module root_mod
program demo
use root_mod
implicit none
call routine_d()
end program demo
c.f90:
submodule(root_mod) c
implicit none
contains
module subroutine routine_c
print *, "hello from routine_c"
end subroutine routine_c
end submodule c
d.f90:
submodule(root_mod) d
implicit none
contains
module subroutine routine_d
print *, "hello from routine_d"
call routine_c()
end subroutine routine_d
end submodule d
把 taskC 放进父模块就会让所有子模块都能访问它,我不希望这样。
Putting taskC in the parent module makes it accessible from every submodule, and that isn't what I want.

Fortran 中同级的子模块不知道彼此的存在。不过,子模块的层次可以按需要一直向下扩展。
module root1
implicit none
...
end module root1
submodule (root1) layer1
implicit none
interface
module subroutine task1() ! visible to descendants of layer1 but not to root1
end subroutine
end interface
end submodule layer1
submodule (root1:layer1) layer2
implicit none
contains
subroutine perform_task()
call task1()
end subroutine
end submodule layer2
submodule (root1:layer1) layer3
implicit none
contains
subroutine perform_task()
call task1()
end subroutine
end submodule layer3
请留意,layer2 和 layer3 都定义了 perform_task,因为同级子模块无法看到彼此的声明。
Sibling Fortran submodules don't know that the others exist. The hierarchy of submodules can, however, be extended as far as you need:
module root1
implicit none
...
end module root1
submodule (root1) layer1
implicit none
interface
module subroutine task1() ! visible to descendants of layer1 but not to root1
end subroutine
end interface
end submodule layer1
submodule (root1:layer1) layer2
implicit none
contains
subroutine perform_task()
call task1()
end subroutine
end submodule layer2
submodule (root1:layer1) layer3
implicit none
contains
subroutine perform_task()
call task1()
end subroutine
end submodule layer3
Observe that both layer2 and layer3 define perform_task, because as siblings they cannot see one another's declarations.
这是我在 GDScript 中使用的枚举声明。 我想随机选择一个枚举变体,而不用列出所有可能的变体。这样以后增加变体时,就不必修改负责随机选择的代码。 我尝试过下面这种方法。 还有这一种。 两种都失败了。一个返回 Null,另一个报告 "Invali…
Nim 中怎样表示求余运算? cell % 7 == 0 会给出 undeclared identifier: '%' 这个错误 Google 搜索和查看 SO 都没找到答案。
怎样才能让函数在执行结束后返回一个值? 例如我有这样的代码。 ROUTINE N ? N ⍴ 12 //它会生成 N 个从 1 到 12 的随机值 (N 是函数的参数)。 我只是想知道怎样拿到函数的结果,然后比如把这个结果传给另一个函数。 谢谢大家的…