[J3] type definitions

Robert Corbett rpcorbett at att.net
Tue Oct 25 22:34:28 UTC 2022


Consider the programs

---------------------------------------

      PROGRAM MAIN
        TYPE T
          REAL(KIND(X)) Y
        END TYPE T
        CALL SUBR1
      CONTAINS
        SUBROUTINE SUBR1
          X = 1
          CALL SUBR2
          PRINT *, X
        END SUBROUTINE SUBR1
        SUBROUTINE SUBR2
          X = 2
          PRINT *, X
        END SUBROUTINE SUBR2
      END

=======================================


      PROGRAM MAIN
        CALL SUBR1
      CONTAINS
        SUBROUTINE SUBR1
          X = 1
          CALL SUBR2
          PRINT *, X
        END SUBROUTINE SUBR1
        SUBROUTINE SUBR2
          X = 2
          PRINT *, X
        END SUBROUTINE SUBR2
      END

---------------------------------------

In the Fortran 2008 standard, as emended by Corrigendum 1,
there is text that specifies that the scope of a variable
that is implicitly declared in a type definition is the
scoping unit in which the type definition appears.
In the first program the scope of the variable X is the
scoping unit that is the main program excluding the
contained subroutines.  The occurrences of X in the
contained routines are host associated with the variable X
in the main program.  In the second program, the occurrences
of X implicitly declare separate variables in the two
contained subroutines.

In Fortran 2018 and draft Fortran 2023, the text that was
added to the Fortran 2008 standard has been dropped.
I find nothing in the text of the current or upcoming
standard that implies that an implicit declaration in a
type definition is handled differently from an implicit
declaration in a procedure.  In the absence of such a
distinction, the current text seems to say that there
are three separate variables with the name X in the
first program.
Bob Corbett

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.j3-fortran.org/pipermail/j3/attachments/20221025/6f60b871/attachment-0001.htm>


More information about the J3 mailing list