Object-Oriented Fortran: User-defined constructors
Rafik_Zurob 270003B2DK Visits (9902)
One of the main features of Fortran 2003 is object-orientation (OO). Derived types can now have specific and generic type-bound procedures, type-bound defined operators, type-bound defined assignment, type-bound finalizers (destructors), and type-bound user-defined derived type IO routines. For example, type base below has specific type-bound procedures find, assign, add and writef, defined operator +, type bound assignment, and a user-defined derived type IO formatted write routine:
module m type base integer, allocatable :: data(:) contains procedure :: find procedure :: assign procedure :: add procedure :: writef generic :: assignment(=) => assign generic :: operator(+) => add generic :: write(formatted) => writef end type !... contains !... end module
If you're familiar with other OO languages, you might be wondering about constructors. Were they missed in Fortran 2003?
The answer is no. Fortran 2003 has support for default and user-defined constructors. The constructors are not type-bound procedures, however. Structure constructors act as default constructors. User-defined constructors are created by defining a generic interface with the same name as the derived type they're supposed to construct. For example:
module m !... interface base module procedure new_base end interface contains !... function new_base(i) integer, intent(in) :: i type(base) new_base allo
The interface above defines a user-defined constructor for type base. It is used in a similar way to a structure constructor. It can even take argument keywords.
use m implicit none type(base) b1, b2, b3 b1 = base(3) ! calls new_base, assign print *, b1 ! calls writef b2 = base(i=4) ! calls new_base, assign print *, b2 ! calls writef b3 = b1 + b2 ! calls add, assign print *, b3 ! calls writef end
I've attached the full example to the files section as "con
For example, on my AIX machine:
> cc -c /usr
Notice how new_base above was called for base(3) and base(i=4).