In this tutorial on Object Oriented Programming in Fortran 2003, we are going to discuss how to create constructors and destructors for a Fortran class. During this tutorial, I assume that you know how to create a new project and what a class looks like in Fortran 2003. This tutorial is build around a TimerClass, which I wrote as an upgrade for my initial timing module in HIVE-tools. The full source of this TimerClass can be found and downloaded from github.
Where the former two tutorials were aimed at translating a scientific model into classes within the confines of the Fortran programming language, this tutorial is aimed at consolidating a class using good practices: The creation a constructor and destructor. As the destructor in Fortran classes is most straight forward of the two, we’ll start with it.
1. The destructor.
A destructor is a method (i.e., a class subroutine) which is automatically invoked when the object is destroyed (e.g., by going out of scope). In case of a Fortran class, this task is performed by the class-method(s) indicated as final procedure. Hence such methods are also sometimes referred to as finalizers. Although in some languages destructors and finalizers are two distinctly different features (finalizers are then often linked to garbage collecting), within the Fortran context I consider them the same.
Within the definition of our TTimerClass the destructor is implemented as:
- module TimerClass
- implicit none
- type, public :: TTimer
- private
- ! here come the properties
- contains
- private
- ! here come the methods
- final :: destructor
- end type TTimer
- contains
- subroutine destructor(this)
- Type(TTimer) :: this
- ! Do whatever needs doing in the destructor
- end subroutine destructor
- end module TimerClass
In contrast to a normal class-method, the destructor is called using the final keyword, instead of the usual procedure keyword. This method is private, as it is not intended to be used by the user anyway, only by the compiler upon cleanup of the instance of the class (i.e., the object). Furthermore, although defined as part of the class, a final subroutine is not type-bound, and can thus not be accessed through the type.
The destructor subroutine itself is a normal Fortran subroutine. There is, however, one small difference with a usual class-method, the parameter referring to the object (c.q. “this“) is indicated as a TYPE and not as a CLASS. This is because the destructor is only applicable to properties belonging to this “class” (Note that final subroutines are not inherited by the child-class). For a child-class (also called a derived class), the destructor of the child-class should deal with all the additional properties of the child-class, while the destructor of the parent-class is called to deal with its respective properties. In practice, the destructor of the child-class is called first, after which the destructor of the parent class is called (and recursively further along the class its family tree.)
So what do you put in such a destructor? Anything that needs to be done to allow the object to be gracefully terminated. Most obviously: deallocation of allocatable arrays, pointer components, closing file handles,…
2. The constructor.
Where other programming languages may provide an initialization section or access to a key-worded constructor. Although Fortran allows for variables to be initialized upon definition, there is no constructor keyword available to be used in its classes. Of course, this does not prevent you from adding an “init()” subroutine which the user should call once the new object is allocated. You could even use a private Boolean property (initialized old style) to keep track of the fact that an object was initialized when entering any of its methods, and if not, call the init() function there and then. There are many ways to deal with the initialization of a new object. Furthermore, different approaches also put the burden of doing things right either with the programmer developing the class, or the user, applying the class and creating objects.
Here, I want to present an approach which allows you to present a clear set-up of your class and which resembles the instance creation approach also seen in other languages (and which implicitly shows the “pointer”-nature of objects ):
NewObject = TClass()
In case of our TTimer class this will look like:
Type(TTimer) :: MyTimer MyTimer = TTimer()
This means we need to have a function with the exact same name as our class (cf., above), which is achieved through the use of an interface to a module procedure. Just giving this name to the constructor function itself will cause your compiler to complain (“Name ttimer at (1) is already defined as a generic interface“). By using a different name for the function, and wrapping it in an interface, this issue is avoided.
- module Timerclass
- implicit none
- type, public :: TTimer
- private
- ...
- contains
- private
- ...
- end type TTimer
- interface TTimer
- module procedure Constructor
- end interface TTimer
- contains
- function Constructor() Result(Timer)
- type(TTimer) :: Timer
- !initialize variables directly
- Timer%x=...
- ! or through method calls
- call Timer%setTime(now)
- ...
- end function Constructor
- end module TimerClass
Note that the constructor function is not part of the class definition, and as such the object is not passed to the constructor function. In addition, the Timer object being created is defined as a Type(TTimer) not Class(TTimer), also because this function is not part of the class definition.
That is all there is to it. Simple and elegant.
In our next Tutorial, we’ll have a look at operator and assignment overloading. Combined with a constructor and destructor as presented here, you are able to create powerful and intuitive classes (even in Fortran).