Emulating namespaces in Fortran 90

后端 未结 3 1306
无人共我
无人共我 2020-12-31 13:56

One of the most troublesome issues with Fortran 90 is the lack of namespacing. In this previous question \"How do you use Fortran 90 module data\" from Pete, it has been dis

3条回答
  •  天命终不由人
    2020-12-31 14:15

    Having several years of Fortran-only programming experience (I got into Python only a year ago), I was not aware of such concept as namespaces for a while. So I guess I learned to just keep track of everything imported, and as High Performance Mark said, use ONLY as much as you have time to do it (tedious).

    Another way I can think of to emulate a namespace would be to declare everything within a module as a derived type component. Fortran won't let you name the module the same way as the namespace, but prefixing module_ to module name could be intuitive enough:

    MODULE module_constants
    IMPLICIT NONE
    
    TYPE constants_namespace
      REAL :: pi=3.14159
      REAL ::  e=2.71828
    ENDTYPE
    
    TYPE(constants_namespace) :: constants
    
    ENDMODULE module_constants
    
    
    PROGRAM namespaces
    USE module_constants
    IMPLICIT NONE
    
    WRITE(*,*)constants%pi
    WRITE(*,*)constants%e
    
    ENDPROGRAM namespaces
    

提交回复
热议问题