Fortran Data Types Assumed and deferred length type parameters


Example

Variables of character type or of a derived type with length parameter may have the length parameter either assumed or deferred. The character variable name

character(len=len) name

is of length len throughout execution. Conversely the length specifier may be either

character(len=*) ...  ! Assumed length

or

character(len=:) ...  ! Deferred length

Assumed length variables assume their length from another entity.

In the function

function f(dummy_name)
  character(len=*) dummy_name
end function f

the dummy argument dummy_name has length that of the actual argument.

The named constant const_name in

character(len=*), parameter :: const_name = 'Name from which length is assumed'

has length given by the constant expression on the right-hand side.


Deferred length type parameters may vary during execution. A variable with deferred length must have either the allocatable or pointer attribute

character(len=:), allocatable :: alloc_name
character(len=:), pointer :: ptr_name

Such a variable's length may be set in any of the following ways

allocate(character(len=5) :: alloc_name, ptr_name)
alloc_name = 'Name'         ! Using allocation on intrinsic assignment
ptr_name => another_name    ! For given target

For derived types with length parameterization the syntax is similar

  type t(len)
    integer, len :: len
    integer i(len)
  end type t

  type(t(:)), allocatable :: t1
  type(t(5)) t2

  call sub(t2)
  allocate(type(t(5)) :: t1)

contains

  subroutine sub(t2)
    type(t(*)), intent(out) :: t2
  end subroutine sub

end