Clicky

Fortran Wiki
Extension to USE statement

An extension to USE statement

Proposal

Extend USE statement in a way to allow USE associated entities to be called only with a defined use-name in the first part.

Rationale

  • At present by “USE module-name” statement all public entities defined in module “module-name” are imported in the current context. However, these USE associated names may be the same names we want to use in our code. In other words, USE associated names “pollute” the current name space. The current solution is to use ONLY option in the USE statement for every entity we want to use with possible renaming of the entities to prevent the name duplication. However, it is not always most convenient solution for the problem.

  • The name space pollution problem is already addressed by Espen Myklebust in his proposal to extend USE statement with access specification. The current proposal complements to the suggested changes.

  • The proposed syntax extension solves this name space pollution problem by allowing the use of additional “use-name” in front of USE associated names. In addition, this syntax extension contributes to readability of the code, because it makes clear from where the used entity came.

  • The proposed syntax is similar to syntax of import statement in Python programming language. The current syntax (USE module-name) corresponds to Python’s from module-name import * statement. While the proposed syntax corresponds to import module-name as name statement.

Proposed syntax

USE [[, module-nature] ::] module-name [AS use-name] [, ONLY:only-list]

Without “AS use-name” it is a regular USE statement. However, when the “AS use-name” is used, then the entities defined in module “module-name” are accessed only with “use-name%entity-name” syntax.

Example

    MODULE hitechlib 
        REAL :: a_var, b_var
        :
    END MODULE

    SUBROUTINE calculate()
        USE hitechlib AS ht  !!!

        ! The same name of variable as defined in “hitechlib” is used here
        REAL :: a_var 

        ! Entities of “ hitechlib” are accessed by using the “use-name” and the percent character.
        a_var = ht%a_var + ht%b_var

        ! An alternative syntax could be to use “.” or “::” characters
        a_var = ht.a_var + ht.b_var
        a_var = ht::a_var + ht::b_var
        :
    END SUBROUTINE