Emulating namespaces in Fortran 90

后端 未结 3 1289
无人共我
无人共我 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:05

    For me this is the most irritating Fortran feature related to modules. The only solution is to add common prefix to procedures, variables, constants, etc. to avoid namespace collisions.

    One can prefix all entities (all public entities seems to be more appropriate) right inside the module:

    module constants
    
      implicit none
    
      real, parameter :: constants_pi = 3.14
      real, parameter :: constants_e = 2.71828183
    
    end module constants
    

    Drawback is increased code verbosity inside the module. As an alternative one can use namespace-prefix wrapper module as suggested here, for example.

    module constants_internal
    
      implicit none
    
      real, parameter :: pi = 3.14
      real, parameter :: e = 2.71828183
    
    end module constants_internal
    
    module constants
    
      use constants_internal, only: &
        constants_pi => pi, &
        constants_e => e
    
    end module constants
    

    The last is a small modification of what you, Stefano, suggested.

    Even if we accept the situation with verbosity the fact that Fortran is not case-sensitive language force us to use the same separator (_) in entities names. And it will be really difficult to distinguish module name (as a prefix) from entity name until we do not use strong naming discipline, for example, module names are one word only.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-31 14:24

    Fortran 2003 has the new ASSOCIATE construct and don't forget the possibility of renaming USE- associated entities. But I don't think that either of these is much closer to providing a good emulation of namespaces than Fortran 90 already has, just (slightly) better workarounds.

    Like some of the respondents to the question you link to, I tend to think that modules with very many identifiers should probably be split into smaller modules (or, wait for Fortran 2008 and use submodules) and these days I almost always specify an ONLY clause (with renames) for USE statements.

    I can't say that I miss namespaces much, but then I've never had them really.

    0 讨论(0)
提交回复
热议问题