Abstract types and deferred bindings
A base type whose only job is to say what its children must do is an abstract type. It has components and bindings like any other, but it also has at least one deferred binding: a name with a shape and no code, which every concrete child has to implement. No value of an abstract type can exist, so the compiler can be sure that every object reached through it has an implementation of every deferred name.
An abstract type
type, abstract :: shape declares the type and forbids values of it. It keeps what concrete types share, here a name component and a show binding that every shape can use, and it leaves the part that differs to the children.
The file abs.f90:
module shapes
implicit none
private
public :: shape, circle, square
type, abstract :: shape
character(len=12) :: name = 'shape'
contains
procedure(area_iface), deferred :: area
procedure :: show => shape_show
end type shape
abstract interface
function area_iface(self) result(a)
import :: shape
class(shape), intent(in) :: self
real :: a
end function area_iface
end interface
type, extends(shape) :: circle
real :: r = 0.0
contains
procedure :: area => circle_area
end type circle
type, extends(shape) :: square
real :: side = 0.0
contains
procedure :: area => square_area
end type square
contains
function circle_area(self) result(a)
class(circle), intent(in) :: self
real :: a
a = 3.14159265 * self%r ** 2
end function circle_area
function square_area(self) result(a)
class(square), intent(in) :: self
real :: a
a = self%side ** 2
end function square_area
subroutine shape_show(self)
class(shape), intent(in) :: self
real :: a
a = self%area()
print '(a,f9.3)', trim(self%name) // ': area =', a
end subroutine shape_show
end module shapes
program absdemo
use shapes
implicit none
class(shape), allocatable :: s
s = circle(name='circle r=2', r=2.0)
call s%show()
s = square(name='square s=3', side=3.0)
call s%show()
end program absdemo
gfortran -Wall -Wextra -std=f2018 abs.f90 -o abs
./abs
circle r=2: area = 12.566
square s=3: area = 9.000
One variable, two dynamic types, one call. The area came from the child each time, while show itself was written once for the parent and never mentions a circle or a square.
The deferred binding
procedure(area_iface), deferred :: area declares a binding with an interface and no implementation. A type that has one is an abstract type even if the abstract keyword is missing, and every non-abstract child must bind a procedure to that name or be abstract itself. The compiler checks the shape of the child's procedure against the interface, so a child that returns an integer is refused where it is declared.
A deferred binding may be nopass, like any other, and it may have extra arguments after the passed object. What it may not have is a body: that is the point of it.
The abstract interface
The interface named in the parentheses of a deferred binding has to be declared, and an abstract interface block is the declaration for a procedure nobody writes. It gives the name, the argument list and the result, and it is used only where a procedure is named. The block sits in the same module, before or after the type, as long as the compiler meets it before the binding that needs it.
Why IMPORT is needed
An interface body is its own scoping unit: names from the module around it are not visible inside, which is what lets a generic interface name a procedure declared anywhere. So the type shape that the dummy argument needs has to be imported by hand, and import :: shape is that line. Leave it out and the compiler reads class(shape) as a type it has never seen:
import with no list imports every name of the host, which is shorter and hides which names the interface really needs. The list form is the one to write: a reader of the interface sees its dependencies in one line.
A concrete child
circle and square are ordinary extensions with one component each and a binding for area. Neither declares abstract, so each one is concrete, and a value of either can be created. The base component name is inherited and set through the constructor, and each child's area function is written for its own type: circle_area takes class(circle).
When a child overrides a deferred binding, the parent's show calls it through the base type and the run picks the child's code. That is the whole design: the shared algorithm is written once against shape, and the one thing it cannot know, the area, is the one thing it asks for.
An object of an abstract type
Declaring a variable of an abstract type is the error the keyword exists to give:
program badabstract
implicit none
type, abstract :: shape
real :: x = 0.0
end type shape
type(shape) :: s
s%x = 1.0
print *, s%x
end program badabstract
gfortran -Wall -Wextra -std=f2018 e1.f90 -o e1
e1.f90:8:18:
8 | type(shape) :: s
| 1
Error: 's' at (1) is of the ABSTRACT type 'shape'
The message is about the declared type, so the fix is never a cast: the variable has to be declared class(shape) and given a concrete dynamic type by allocation or by assignment. This example has no deferred binding, which is why it also shows that abstract alone is enough to forbid a value.
What an abstract type is for
The pattern is the one every object-oriented language has: an interface written once, several implementations, and code that takes the interface. In Fortran the interface is an abstract type because the language has no separate notion of one, and the cost is that the base type must also carry whatever components the implementations share.
The return on that cost is a library that can grow without a rebuild of its users: a new shape is a new module that extends shape, and every procedure written against class(shape) accepts it. A factory function that reads a name and returns the right child makes the growth invisible to the caller; the next lesson looks at types that carry their own precision instead.
Exercises
- Add a child type, extends(shape) :: rect with width and height. What must it declare for the module to compile?
- Remove import :: shape from the interface and read the error. Which name does the compiler complain about?
- Make area_iface a function with an extra integer argument n and give each child that argument. Which call of area stops compiling?
- Add a third deferred binding perimeter to shape and implement it in both children. What happens to a child that is left without one?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
type, abstract :: shapemean?Answer
No value of the type may be declared, only values of its children
An abstract type is a base for others, not an object.
-
What is a DEFERRED binding?
Answer
A name with an interface and no implementation, which a child must provide
The shape is fixed in the parent and the code is the child's.
-
Why does the abstract interface need
import :: shape?Answer
An interface body does not inherit the names of the module around it
An interface body is its own scoping unit.
-
What does this program print?
module m implicit none type, abstract :: base contains procedure(iface), deferred :: value end type base abstract interface integer function iface(self) import :: base class(base), intent(in) :: self end function iface end interface type, extends(base) :: seven contains procedure :: value => seven_value end type seven contains integer function seven_value(self) class(seven), intent(in) :: self seven_value = 7 end function seven_value end module m program p use m implicit none class(base), allocatable :: o o = seven() print *, o%value() end program pAnswer
7
The only concrete child returns seven.
-
What happens to a non-abstract child that does not implement a deferred binding?
Answer
The module does not compile; it must implement it or be abstract
Every concrete type must be complete.
-
What does gfortran report for
type(shape) :: swhen shape is abstract?Answer
Error: ... is of the ABSTRACT type ...
A value of an abstract type cannot exist.
-
How is a variable that can hold any child declared?
Answer
class(shape), allocatable :: s
A polymorphic allocatable holds a concrete dynamic type.
-
Where must the abstract interface of a deferred binding be declared?
Answer
In the same module, before the compiler needs it
The binding names it, so it has to be visible there.
-
What does a child need to become concrete?
Answer
A binding for every deferred name, with a matching interface
The compiler checks the implemented shape.
-
Why is the shared algorithm written against
class(shape)?Answer
So every child is accepted and the deferred call dispatches
One procedure, one call site, any number of children.
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