Derived Types and Allocatable
Allocatable in Derived Type
A derived type can hold an allocatable component, giving a bag-of-values with runtime size.
Program
Play the program to allocate and fill an inner array, then sum it.
allocatable_in_type.f90
program allocatable_in_type_demo
implicit none
type :: bag
integer, allocatable :: items(:)
end type bag
type(bag) :: b
allocate(b%items(3))
b%items = [5, 7, 9]
print '(I0)', sum(b%items)
end program allocatable_in_type_demo
allocatable component
`integer, allocatable :: items(:)` makes the component sized at run time.
allocate component
`allocate(b%items(3))` allocates inside the instance.
array assign
`b%items = [5, 7, 9]` fills the allocated array in one statement.