Quantcast
Channel: Intel® Fortran Compiler
Viewing all articles
Browse latest Browse all 3270

Structure versus TYPE

$
0
0

I stumbled upon this a couple of weeks ago...

I have a lot of reused code (via INCLUDE) which uses the STRUCTURE and then RECORD in the routines. I am not aware that RECORD can make use of the INTENT() argument, so I prefer to use TYPE.
While modifying some code I declared it as STRUCTURE in the subroutine and used TYPE to reference the STRUCTURE. I was not really intending to that, but when it compiled and ran it got me thinking...

Is it intended and OK to make use of the STRUCTURE and reference those using TYPE? It is certainly handy to be able to not have to change the INCLUDEd files, but be able to use INTENT.

Code looks like:

      PROGRAM XX
      IMPLICIT NONE
!<Old Code>
      STRUCTURE POSITION
        REAL(KIND=8) :: X, Y, Z
      END STRUCTURE
      RECORD /POSITION/  Here, There

!<New Code>
!TYPE POSITION_TYPE
!  REAL(KIND=8) :: X
!  REAL(KIND=8) :: Y
!  REAL(KIND=8) :: Z
!END TYPE POSITION_TYPE
!      TYPE(Position_Type) :: Here
!      TYPE(Position_Type) :: There

      CALL XXX(Here, There, Distance)

      END PROGRAM XX

      SUBROUTINE XXX (Here, There, Distance)
!      <Old Code>
      STRUCTURE POSITION
        REAL(KIND=8) :: X, Y, Z
      END STRUCTURE      RECORD /POSITION/  Here, There

      TYPE(POSITION), INTENT(IN   ) :: Here
      TYPE(POSITION), INTENT(IN   ) :: There
      TYPE(POSITION), INTENT(  OUT) :: Distance
      Distance = (&      (Here%X - There%X)**2 +&      (Here%Y - There%Y)**2 +&      (Here%Z - There%Z)**2    )**0.5

      END SUBROUTINE XXX

 


Viewing all articles
Browse latest Browse all 3270

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>