FINAL statement

Standard: F77 F90 F95 F2003 F2008 F2018 Example program


<Syntax>

The FINAL statement declares one or more final subroutines for a derived type; it only appears in the “type-bound procedure part” of a type definition. When an object of the type ceases to exist, one of these final subroutines will be called.

final-procedure-stmt ::= FINAL [ :: ] final-subroutine-name-list
final-subroutine-name-list ::= final-subroutine-name [ , final-subroutine-name ]...

Each final subroutine must be the name of a module procedure with exactly one argument; that argument must be a non-allocatable, non-optional, non-pointer, non-polymorphic variable of the derived type being defined, and must not have the INTENT(OUT) or VALUE attribute. No two final subroutines of a type may have a dummy argument with the same rank.

<Semantics>

When an object of the type is finalised, if there is a final subroutine that accepts the rank of the object that final subroutine will be called with the object as the actual argument. A final subroutine is permitted to be elemental, in which case it will be called if there is no other final subroutine for the rank of the object.

An object is finalised when it

If a component is finalisable, any final subroutine for the object as-a-whole will be called before finalising any component. If the parent object is an array, each element has its components finalised separately; the final subroutine called is the one for the rank of the component, not the rank of the object.

If an object of extended type has a finalisable parent type, the parent is finalised after calling any final subroutine for the extended type itself (and after calling any final subroutine for the extended components).

Note that although final subroutines are declared in the type-bound procedure section, they are not type-bound procedures: they are not callable through the type-bound procedure calling mechanism, and they are not inherited.

<Related>

BLOCK construct, CLASS specifier, Component definition statement, Derived Type Definition, EXTENDS clause, TYPE statement