A derived type (Fortran 90) groups components into one type. Fortran 2003 added type extension, polymorphism and procedures bound to types, which together are Fortran’s object-oriented features.
Defining a type
type :: point
real :: x = 0.0, y = 0.0 ! default initial values
end type point
type, public :: particle
type(point) :: position
real, allocatable :: history(:) ! allocatable component (Fortran 2003)
real, pointer :: link => null()
character(len=16) :: name = ''
integer, private :: id ! hidden outside the module
end type particle
type(particle) :: p
p%position%x = 1.5
q = point(y = 2.0, x = 1.0) ! structure constructor, components by keyword
- components are accessed with
%; a type name and its components are plain names, and the viewer colours the name aftertype,extends(andtype(as a type sequencekeeps the components in the order written, so that the type can be incommonor be passed to C-unaware code;bind(c)makes it C-compatibleprivatebefore the components hides them outside the module;type, privatehides the whole type- a structure constructor has the type’s name and a value for each component (or
name=keywords, and default values may be left out); it can be a constant when its parts are - type parameters (Fortran 2003):
type :: buffer(k, n)withinteger, kind :: kandinteger, len :: n;type(buffer(4, 100)) :: b
Extension and abstract types
type, abstract :: shape
contains
procedure(area_fn), deferred :: area ! no body: children must provide it
end type shape
abstract interface
function area_fn(self) result(a)
import :: shape
class(shape), intent(in) :: self
real :: a
end function area_fn
end interface
type, extends(shape) :: circle
real :: radius
contains
procedure :: area => circle_area ! binds the name area to this procedure
end type circle
extends(parent)adds components to the parent; the child contains an inherited component named for the parentabstracttypes cannot be made into objects; they holddeferredbindings that each non-abstract descendant overridesnon_overridableon a binding stops children from replacing it;final :: routinenames a subroutine the compiler runs when an object is destroyed (Fortran 2003)
Type-bound procedures
type :: account
real :: balance = 0.0
contains
procedure :: deposit ! the procedure is named deposit
procedure, pass(self) :: report ! self is the passed-object dummy
procedure, nopass :: bank_name ! no passed object
generic :: add => deposit, deposit_many ! a generic binding
generic :: operator(+) => merge_accounts
generic :: assignment(=) => copy_from
generic :: write(formatted) => write_account ! defined output
final :: close_account
end type account
call acct%deposit(10.0) ! acct is passed as the first argument
- the object is passed as the passed-object dummy, by default the first argument, declared
class(account);pass(name)picks another, andnopasspasses none procedure :: namebinds a procedure of that name;procedure :: name => otherbinds another procedure undernamegenericbindings name several specific bindings, and may beoperator(…),assignment(=)or user-defined input/output (read(formatted),write(unformatted))- bindings are
publicunless the type saysprivateaftercontains;procedure(interface), pointeras a component (nocontains) is a procedure pointer component instead
Polymorphism
class(shape), allocatable :: s ! may hold a circle or any other shape
class(shape), pointer :: p
class(*), allocatable :: anything ! unlimited polymorphic
allocate(circle :: s) ! choose the dynamic type
allocate(s, source=other_shape) ! copy type and value
select type (s)
type is (circle)
print *, s%radius
class is (shape)
print *, s%area()
end select
print *, same_type_as(a, b), extends_type_of(a, b)
- a
class(t)variable may be of typetor of any type extended from it. Calls through it use the dynamic type’s binding (dynamic dispatch) class(*)may hold a value of any type; onlyselect typeand the intrinsicssame_type_as,extends_type_ofandstorage_sizecan use itselect typebranches on the dynamic type (Statements and control flow);associategives the selected object a name of the type of its guardallocate(…, source=…)andmold=allocate polymorphic variables from another object or from a type
More
- Enumerations:
enum, bind(c)(Fortran 2003) and theenumeration typeof Fortran 2023 (see Types and declarations) - DEC structures (
structure,record,union,map) predate derived types and are an extension; see Extensions and dialects - Types of the parallel features:
team_type,event_typeandlock_typeofiso_fortran_envare derived types (Parallel features)