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에서 일일이 나열하지 않고 enum 항목을 무작위로 고르기?

    GDScript에서 enum을 이렇게 선언했어요: 가능한 모든 변형을 나열하지 않고 enum 변형을 무작위로 고르고 싶어요. 나중에 변형을 더 추가해도 무작위로 선택하는 코드를 수정하지 않도록요. 이런 방법을 시도해 봤어요:…

    답글 2최근 활동 gdscript
  2. Nim 나머지 연산 문법

    Nim에서는 나머지를 어떻게 표현하나요? cell % 7 == 0를 쓰면 undeclared identifier: '%'가 나옵니다 Google로 검색해도 SO를 찾아봐도 답을 찾지 못했습니다.

    답글 2최근 활동 nim
  3. APL 함수에서 결과 반환하기

    함수 실행이 끝나면 어떤 값을 반환하게 하려면 어떻게 하나요? 예를 들어 이런 것이 있습니다: ROUTINE N ? N ⍴ 12 //이것은 1부터 12까지의 난수 N개를 만듭니다 (N은 함수의 인수입니다). 함수의 결과를 돌…

    답글 2최근 활동 apl