Parameterised derived types

A type whose component is an array of ten reals is ten reals wide, and a program that needs twelve needs another type. A parameterised derived type takes the width as a parameter: type :: buffer(k) takes a kind, type :: line(n) takes a length, and type(line(20)) is one of the family. This lesson writes both kinds of parameter, allocates a value whose length is decided at run time, and says plainly which part of the feature this compiler supports.

A type with a parameter

The parameter list follows the type name, and each parameter is declared inside the type as integer, kind or integer, len. A kind parameter may be used in the declaration of a component, a len parameter may be used as the bound of an array or the length of a character, and every use of the type has to supply a value that the compiler can fold at compile time.

The file kind.f90:

program kinds use iso_fortran_env, only: real32, real64 implicit none type :: buffer(k) integer, kind :: k real(kind=k) :: data(4) end type buffer type(buffer(real32)) :: b4 type(buffer(real64)) :: b8 b4%data = 1.0 b8%data = 2.0_real64 print '(a,i0,a,f8.1)', 'k = ', kind(b4%data), ' sum = ', sum(b4%data) print '(a,i0,a,f8.1)', 'k = ', kind(b8%data), ' sum = ', sum(b8%data) end program kinds

gfortran -Wall -Wextra -std=f2018 kind.f90 -o kind
./kind

k = 4 sum = 4.0
k = 8 sum = 8.0

One type, two kinds, and the component is a different type in each: kind(b4%data) is four and kind(b8%data) is eight, which is the whole point of a kind parameter. The two are distinct types with distinct interfaces, so a procedure that takes type(buffer(real32)) cannot be called with a b8.

A kind parameter

A kind parameter is written integer, kind :: k and used as real(kind=k), integer(kind=k) or complex(kind=k). It is what a numerical library wants when the same container has to hold single or double precision, because one source text then serves both and the compiler generates two types.

A kind parameter may have a default, integer, kind :: k = real32, and then a use may leave it out. Defaults are where this build is weakest: a type with two parameters, one kind and one len, is refused even when both have defaults, and every kind parameter of this compiler's version has to be written out at each use. The next section shows what does work.

A len parameter

A len parameter is written integer, len :: n and used as an array bound or a character length. The type line(n) holds one string of exactly n characters, and the program below makes three lines of three different lengths, one of them allocated at run time.

The file len.f90:

module bufs implicit none private public :: line, show_line type :: line(n) integer, len :: n character(len=n) :: text end type line contains subroutine show_line(l, label) type(line(*)), intent(in) :: l character(len=*), intent(in) :: label print '(a,a,a,a,i0,a,i0)', trim(label), ': [', trim(l%text), '] n = ', l%n, ' used = ', len_trim(l%text) end subroutine show_line end module bufs program lendemo use bufs implicit none type(line(8)) :: short type(line(20)) :: long type(line(:)), allocatable :: any short%text = 'alpha' long%text = 'a longer line' call show_line(short, 'short') call show_line(long, 'long ') allocate(line(12) :: any) any%text = 'allocated' call show_line(any, 'any ') end program lendemo

gfortran -Wall -Wextra -std=f2018 len.f90 -o len
./len

short: [alpha] n = 8 used = 5
long: [a longer line] n = 20 used = 13
any: [allocated] n = 12 used = 9

Each line tells the same story: the parameter is the stored length, the string is padded to it, and len_trim gives the part that carries characters. Reading l%n inside the procedure is legal; writing it is the error of the section after next.

An allocated parameterised value

type(line(:)), allocatable :: any is a variable with a deferred len parameter: the length is decided when it is allocated. allocate(line(12) :: any) fixes it at twelve, and the length then stays with the value for its whole life. Deallocating and allocating again with another parameter gives a different type in the same variable.

This is the reason the feature exists: a collection type, a string type or a matrix type can be one declaration and its size a value, instead of one type per size or an allocatable component with a separate bookkeeping integer. The deferred parameter is what the colon means, and it is only allowed on an allocatable, a pointer or a dummy argument.

TYPE(LINE(*))

type(line(*)), intent(in) :: l in show_line is an assumed-length parameter: the dummy accepts a line of any length and reads the length from the actual argument, which is why l%n and l%text both work inside. It is the parameterised equivalent of character(len=*) and it is the form a procedure that only reads a value should take.

An assumed-shape array of a parameterised type needs an explicit interface, like every assumed-shape argument, so the procedure goes in a module. That is the same rule as part 4 and it is the reason the example has a module at all.

What this build supports

gfortran 15.2.0 supports part of this feature, and a page that hid that would waste a reader's afternoon. Three findings from this container:

A type with a kind parameter and a len parameter together does not compile: Error: The value for the KIND parameter 'k' at (1) does not reduce to a constant expression. The same type with both parameters dropped and a literal kind works, and a type with only a len parameter works, so the limit is the combination.

len() applied to a component of a parameterised type stops the compiler with f951: internal compiler error: Segmentation fault. len_trim on the same component is fine, which is why show_line prints the used length with it.

Neither finding is a reason to avoid the feature: the length parameter is the part that pays, it works, and the kind parameter works on its own. A program that needs both should keep the precision in the component type and the size in an allocatable component, which is the arrangement of section 072.

Assigning a parameter

A parameter is part of the type, not a component with storage, and the compiler refuses to write it:

program badparam implicit none type :: line(n) integer, len :: n character(len=n) :: text end type line type(line(8)) :: short short%n = 20 short%text = 'alpha' print *, short%text end program badparam

gfortran -Wall -Wextra -std=f2018 badparam.f90 -o badparam

badparam.f90:11:3:

11 | short%n = 20
| 1~~~~~~
Error: The assignment to a KIND or LEN component of a parameterized type at (1) is not allowed

The caret spans the whole designator, and the message says which two kinds of component may not be assigned. A value that needs another length is another value: type(line(20)) :: longer, or an allocatable with a new allocate.

When a parameterised type pays

A parameterised type pays when a family of types differs only in a precision or a size and the code that uses them is written once. It pays less when the size is known at the point of use, because real :: data(n) with n a parameter already gives one type per size, and less again when the value comes from a file, where an allocatable component is the only honest option.

The feature also has a social cost: a reader who meets type(matrix(real64, 3, 3)) in a signature has to know three things the declaration does not spell out. A named constant for each combination, type(matrix33) as an alias through a separate type, or a plain allocatable component are all easier to read, and the choice is a matter of taste rather than of speed.

Exercises

  1. Add a second component integer :: used = 0 to line. Does the parameter change?
  2. Allocate any again with line(4) after the first allocation. What does n become, and what happens to the old text?
  3. Declare type(line(8)), allocatable :: s and call allocate(line(12) :: s). Read the error.
  4. Change type(line(*)) in show_line to type(line(8)). Which call stops compiling?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is a kind parameter of a derived type used for?

    Answer

    It sets the kind of a component declared with real(kind=k)

    One declaration, one type per precision.

  2. What does type :: line(n) with integer, len :: n declare?

    Answer

    A type whose length parameter sizes a component

    The len parameter is used as a bound or a character length.

  3. What does type(line(:)), allocatable :: any mean?

    Answer

    The length parameter is deferred and fixed by ALLOCATE

    The colon defers the parameter to the allocation.

  4. What does this program print? module m implicit none type :: box(n) integer, len :: n integer :: v(n) end type box end module m program p use m implicit none type(box(3)) :: b b%v = [1, 2, 3] print *, b%n, sum(b%v) end program p

    Answer

    3 6

    n is three and the components add to six.

  5. What does type(line(*)) accept in a dummy argument?

    Answer

    A line of any length, read from the actual argument

    The asterisk is the assumed-length parameter.

  6. What happens on short%n = 20?

    Answer

    It is a compile error: a parameter cannot be assigned

    A parameter is part of the type, not of the storage.

  7. What does gfortran 15.2.0 do with a type that has both a kind and a len parameter?

    Answer

    It refuses: the value for the KIND parameter does not reduce to a constant expression

    The lesson names the limit and the two forms that do work.

  8. What should a procedure that only reads a parameterised value take?

    Answer

    A TYPE(type_name(*)) dummy

    The asterisk accepts every length of the family.

  9. When is a parameterised type less useful than an allocatable component?

    Answer

    When the size is only known at run time from data

    A deferred parameter is fixed by the allocation, not by the data.

  10. Are type(buffer(real32)) and type(buffer(real64)) the same type?

    Answer

    No: different parameter values make different types

    The parameter is part of the type identity.

The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev