oopfortranabstract-classfinalization

How to force the definition of a destructor


I would like to force inheriting classes to define a destructor in Fortran >= 2008.

So far I have tried several variations of:

type, abstract :: parent_class_t
contains
    final, deferred, pass :: cleanup
end type

which don't even compile. How do I achieve that goal?


Solution

  • Final bindings are NOT inherited. Every type can declare its own. When a an child derived type is finalized, the parent finalization procedure is called automatically. Abstract types cannot have a final subroutine because it would not make sense.

    The standard (2018 draft) contains this explanation note:

    NOTE 7.46 Final subroutines are not inherited through type extension and cannot be overridden. The final subroutines of the parent type are called after any additional final subroutines of an extended type are called.

    As francescalus notes, the syntax is:

    R753 final-procedure-stmt is FINAL [ :: ] final-subroutine-name-list

    no attributes may be specified.