Type-bound procedures
A type and the procedures that work on it are one idea, and Fortran lets them be written together: a contains section inside the type lists bindings, and a binding is a name a value of the type carries. The call becomes v%norm() instead of norm(v), the argument is the value in front of the percent sign, and the compiler checks it against the type rather than against a name.
CONTAINS in a TYPE
The type block ends its component list with contains and then one procedure statement per binding. Each line gives the binding a name and, after =>, the module procedure that implements it; procedure :: norm => vec_norm makes %norm the name a user writes and vec_norm the name of the code. Without the arrow the binding takes the name of the procedure.
The file tb.f90:
module vecs
implicit none
private
public :: vec
type :: vec
real :: x = 0.0
real :: y = 0.0
contains
procedure :: norm => vec_norm
procedure :: scale => vec_scale
procedure, nopass :: origin => vec_origin
procedure :: show => vec_show
end type vec
contains
function vec_norm(self) result(r)
class(vec), intent(in) :: self
real :: r
r = sqrt(self%x**2 + self%y**2)
end function vec_norm
subroutine vec_scale(self, k)
class(vec), intent(inout) :: self
real, intent(in) :: k
self%x = self%x * k
self%y = self%y * k
end subroutine vec_scale
function vec_origin() result(v)
type(vec) :: v
v = vec(0.0, 0.0)
end function vec_origin
subroutine vec_show(self, label)
class(vec), intent(in) :: self
character(len=*), intent(in) :: label
print '(a,a,f6.1,a,f6.1,a)', trim(label), ' (', self%x, ', ', self%y, ')'
end subroutine vec_show
end module vecs
program tbdemo
use vecs
implicit none
type(vec) :: v, w
v = vec(3.0, 4.0)
call v%show('v =')
print '(a,f8.3)', 'v%norm() = ', v%norm()
call v%scale(2.0)
call v%show('after x2 =')
w = v%origin()
call w%show('origin() =')
end program tbdemo
gfortran -Wall -Wextra -std=f2018 tb.f90 -o tb
./tb
v = ( 3.0, 4.0)
v%norm() = 5.000
after x2 = ( 6.0, 8.0)
origin() = ( 0.0, 0.0)
Four calls, and every one of them names the value first: v%norm() is a function of one argument, v%scale(2.0) is a subroutine whose first argument is the object, and v%origin() takes no argument at all because its binding is nopass.
The passed-object dummy
The first dummy argument of the implementing procedure is the passed-object dummy: it receives the value written before the percent sign, and it is never written at the call. It is declared class(vec), intent(in) :: self rather than type(vec), because a binding has to work for a type that extends this one too, which is the subject of section 075.
Declaring it real instead breaks the rule at the binding, and gfortran names the offending procedure: Error: Non-polymorphic passed-object dummy argument of 'vec_norm' at (1). The other rules are that the dummy must be declared in the same module and must be the first argument; a binding whose procedure does not fit is rejected where it is named, not where it is called.
The call with %
v%norm() is the whole call; there is no call in front because a function returns a value, while call v%scale(2.0) keeps the keyword a subroutine call always has. The percent sign is the same one that reads a component, so v%x is a value and v%norm is a procedure: both are members of the record.
A binding is not a pointer: the procedure is known at compile time, so the call is a direct one and costs nothing at run time. What is polymorphic is the object, not the call, and only a class() dummy can dispatch to another implementation.
NOPASS
procedure, nopass :: origin => vec_origin binds a procedure that ignores the object. v%origin() still names the value, but the call passes nothing: it is a constructor-like function that happens to live in the namespace of the type. That is how a type offers a named alternative to the structure constructor, and a reader who sees v%origin() knows where to look for it.
Private bindings
A binding may be private even when the type is public. The procedure still exists and the module's own code may call it through the object; a user of the module may not, and the compiler says so at the call:
module vecs
implicit none
public :: vec
type :: vec
real :: x = 0.0
real :: y = 0.0
contains
procedure, private :: scale => vec_scale
end type vec
contains
subroutine vec_scale(self, k)
class(vec), intent(inout) :: self
real, intent(in) :: k
self%x = self%x * k
end subroutine vec_scale
end module vecs
program badprivate
use vecs
implicit none
type(vec) :: v
v = vec(3.0, 4.0)
call v%scale(2.0)
print *, v%x
end program badprivate
gfortran -Wall -Wextra -std=f2018 e3.f90 -o e3
e3.f90:24:14:
24 | call v%scale(2.0)
| 1
Error: 'scale' of 'vec' is PRIVATE at (1)
The message names both the binding and the type, which is what makes this error easy to fix: the name exists, it is in the right place, and the access level is the whole problem. A private binding with a public wrapper procedure in the module is the usual pattern when the wrapper has checks or a nicer signature.
What a binding buys
The procedure is found by the type, so a user of the module does not have to import four names and remember which procedure goes with which type. Tabbing through a value in an editor now lists its members, and the documentation of the type is one page. And the binding is the only way a call can dispatch on a polymorphic object: a plain interface cannot choose an implementation at run time.
What it does not buy is speed, inheritance or a hidden this: the object is an ordinary dummy argument, and the module is still in charge of access. Some projects use bindings for every procedure and some use none; both styles are in the wild, and the third one, a plain procedure that takes the type as its first argument, is the style this course used until this lesson and the one that composes best with a procedure-pointer table.
Exercises
- Add a binding halve that divides both components by two and call it on v. Does it need intent(inout)?
- Rename %norm to %length in the type and in the program. What happens if you rename only one of them?
- Give vec_origin the self argument it does not use, and remove nopass. What does v%origin() then mean?
- Make %show private and add a public subroutine print_vec(v) in the module that calls it. Which line of the program changes?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
containsinside a TYPE introduce?Answer
The bindings: the procedures a value of the type carries
CONTAINS starts the binding list of the type.
-
What is
procedure :: norm => vec_norm?Answer
A binding named norm implemented by vec_norm
The name before the arrow is what a caller writes.
-
What is the passed-object dummy?
Answer
The first dummy of the procedure, which receives the value before the %
It is never written at the call.
-
What does this program print?
module m implicit none type :: counter integer :: n = 0 contains procedure :: bump => bump_impl end type counter contains subroutine bump_impl(self, k) class(counter), intent(inout) :: self integer, intent(in) :: k self%n = self%n + k end subroutine bump_impl end module m program p use m implicit none type(counter) :: c call c%bump(5) call c%bump(2) print *, c%n end program pAnswer
7
The two calls add five and two to the counter.
-
Why is the passed-object dummy declared
class(vec)and nottype(vec)?Answer
A binding must also work for a type that extends this one
A non-polymorphic passed-object dummy is rejected.
-
How is a subroutine binding called?
Answer
call v%scale(2.0)
A subroutine call keeps CALL, as it always does.
-
What does a
nopassbinding mean?Answer
The procedure is bound to the type but receives no object
The call names the value but passes nothing to it.
-
What does gfortran report for a call to a private binding?
Answer
Error: ... is PRIVATE at (1)
The name exists and only its access level fails.
-
What does a binding cost at run time?
Answer
Nothing: it is a direct call known at compile time
Only a class() dummy makes the call polymorphic.
-
Which is NOT a reason to bind a procedure to a type?
Answer
It makes the call faster than a module procedure
A binding is a direct call, the same cost as a module call.
The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev