openlang

在同级子模块间调用过程

如下所示,父模块和各个子模块分别放在单独的文件里。子模块 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

从英语翻译查看原文

后续讨论 3

顺着彼此的依据读下去
  1. 固定链接

    做两处修改就能互相调用。

    • 把子过程的接口放进父模块

    • 不需要 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
    从英语翻译查看原文
  2. 固定链接

    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,因为同级子模块无法看到彼此的声明。

    从英语翻译查看原文

同一个问题的其他角度

  1. 在 Godot 3.3 的 GDScript 中怎样随机选择枚举项而不用逐项列出?

    这是我在 GDScript 中使用的枚举声明。 我想随机选择一个枚举变体,而不用列出所有可能的变体。这样以后增加变体时,就不必修改负责随机选择的代码。 我尝试过下面这种方法。 还有这一种。 两种都失败了。一个返回 Null,另一个报告 "Invali…

    回复 2最近活动 gdscript
  2. Nim 求余语法

    Nim 中怎样表示求余运算? cell % 7 == 0 会给出 undeclared identifier: '%' 这个错误 Google 搜索和查看 SO 都没找到答案。

    回复 2最近活动 nim
  3. 从 APL 函数返回结果

    怎样才能让函数在执行结束后返回一个值? 例如我有这样的代码。 ROUTINE N ? N ⍴ 12 //它会生成 N 个从 1 到 12 的随机值 (N 是函数的参数)。 我只是想知道怎样拿到函数的结果,然后比如把这个结果传给另一个函数。 谢谢大家的…

    回复 2最近活动 apl