Parameterized Derived Types & Submodules
Compiler support for PDTs historically varies; test before relying on them.
24.1 Parameterized derived types (PDTs)
Compiler support for PDTs historically varies; test before relying on them.
type :: matrix(k, rows, cols)
integer, kind :: k = kind(1.0) ! compile-time parameter
integer, len :: rows, cols ! run-time (length) parameters
real(k) :: data(rows, cols)
end type matrix
type(matrix(k=8, rows=3, cols=3)) :: m3
type(matrix(rows=:, cols=:)), allocatable :: mdyn
allocate(matrix(rows=10, cols=20) :: mdyn)
print *, m3%rows, m3%k24.2 Submodules — separating interface from implementation (F2008)
Benefits: changing the implementation doesn't recompile users of the module (avoids compilation cascades), hides internals, breaks circular dependencies.
! geometry.f90 — interface only
module geometry
implicit none
interface
module function area(r) result(a)
real, intent(in) :: r
real :: a
end function area
end interface
end module geometry
! geometry_impl.f90 — implementation
submodule (geometry) geometry_impl
contains
module procedure area
a = 3.14159 * r**2
end procedure area
end submodule geometry_impl24.3 Enumerations
enum, bind(c) ! C-compatible enumerators (F2003)
enumerator :: red = 1, green, blue
end enum
! Fortran 2023: typed enumeration
enumeration type :: color
enumerator :: red, green, blue
end enumeration type