Derived types are User-defined data types that allows to create complex data structures. It is defined with the TYPE statement. In addition, two different derived types exists:
- Non-parametrized: fixed size and type
- Parametrized (PDT): flexible size and type
The use of PDT’s derived types can be useful in several ways:
- Less code to deal
- allocatable arrays
- Similar types with different characteristics
- Opportunities for compiler optimizations
Its use allows to parametrize the following properties of an array:
- length: noted as LEN
- type: noted as kind
User types can be parametrized in the following way:
TYPE t_sf_prob_mort(nc)
INTEGER, LEN :: nc
REAL(wp) :: fire(nc) !< mix of tau and ck [-]
REAL(wp) :: tau(nc) !< probability of post fire mortality due to cambial damage [-]
REAL(wp) :: ck(nc) !< probability of post fire mortality due to crown damage [-]
CONTAINS
PROCEDURE :: calc
END TYPE t_sf_prob_mort
Initialized as:
TYPE(t_sf_prob_mort(nc)) :: prob_mort
Type-bound procedures
When including a type-bound procedure, it must be written in the following way:
SUBROUTINE calc(this)
CLASS(t_sf_prob_mort(*)), INTENT(in) :: this
! do something here
END SUBROUTINE calc
Be aware some compilers may still have issues regarding the implementation of this Fortran feature.
Source: Intel 2023