Beliavsky / FortranTip

Short instructional Fortran codes associated with Twitter @FortranTip
https://zmoon.github.io/FortranTipBrowser/
The Unlicense
64 stars 14 forks source link

Tip: derived type can have derived type components #31

Open Beliavsky opened 2 years ago

Beliavsky commented 2 years ago

A derived type may have components that are also derived types. If x is an instance of a derived type that has component y, and y has component z, you can write x%y%z to access z. A similar syntax applies with type extension of Fortran 2003.

module m
implicit none
type :: date_t
   integer :: year, month, day
end type date_t
type :: daily_temperature_t
   type(date_t) :: date
   real :: high, low
end type daily_temperature_t
end module m
!
program derived_type_composition
use m, only: daily_temperature_t, date_t
implicit none
type(daily_temperature_t) :: x
character (len=*), parameter :: fmt = "(*(1x,g0))"
! use default constructors of daily_temperature_t and date_t
x = daily_temperature_t(date_t(2022,2,12),high=30.0,low=10.0)
print fmt,x ! 2022 2 12 30.0000000 10.0000000
print fmt,x%date%year ! 2022
end program derived_type_composition