EXTENDS and CLASS()

A three-dimensional point is a two-dimensional point with one more number, and Fortran lets that be a statement rather than a copy of the type. type, extends(point) :: point3 declares a type that has every component of point and the components written under it. The child is a point for every purpose that uses only the parent part, and a procedure declared with a class(point) dummy accepts both.

EXTENDS

The parent is named in the extends clause, the child keeps the parent's components and adds its own, and the parent type itself is unchanged: it does not know it has a child. The child type is a different type with a different constructor, and a value of one is not a value of the other unless a class() declaration says so.

The file inh.f90:

module shapes implicit none private public :: point, point3, describe type :: point real :: x = 0.0 real :: y = 0.0 contains procedure :: norm => point_norm procedure :: show => point_show end type point type, extends(point) :: point3 real :: z = 0.0 end type point3 contains function point_norm(self) result(r) class(point), intent(in) :: self real :: r r = sqrt(self%x**2 + self%y**2) end function point_norm subroutine point_show(self, label) class(point), 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 point_show subroutine describe(p, label) class(point), intent(in) :: p character(len=*), intent(in) :: label print '(a,a,f6.1,a,f6.1,a,f8.3)', trim(label), ' xy = (', p%x, ', ', p%y, ') norm = ', p%norm() end subroutine describe end module shapes program inhdemo use shapes implicit none type(point) :: p type(point3) :: q p = point(3.0, 4.0) q = point3(1.0, 2.0, 2.0) call p%show('p =') call q%show('q (inherits)=') print '(a,f8.3)', 'p%norm() = ', p%norm() print '(a,f8.3)', 'q%norm() = ', q%norm() print '(a,f6.1)', 'q%z = ', q%z call describe(p, 'describe p:') call describe(q, 'describe q:') end program inhdemo

gfortran -Wall -Wextra -std=f2018 inh.f90 -o inh
./inh

p = ( 3.0, 4.0)
q (inherits)= ( 1.0, 2.0)
p%norm() = 5.000
q%norm() = 2.236
q%z = 2.0
describe p: xy = ( 3.0, 4.0) norm = 5.000
describe q: xy = ( 1.0, 2.0) norm = 2.236

The child answered a binding it never declared, printed the parent part of itself, and went through a procedure written for the parent. q%norm() is sqrt(1 + 4) and not sqrt(1 + 4 + 4): the binding was written for point and the compiler only lets it read the parent part. That is the point of the next lesson, where the child gets its own version of the same binding.

What a child inherits

Every component of the parent, with its default, and every binding of the parent, unless the child rebinds the name. The child may add components, add bindings, and override a binding; it may not remove a component or make a public binding private. The parent part of a child value is laid out first, which matters for a C interoperable type and for the order a constructor takes.

The parent is not copied at run time: a type(point3) value holds its own x and y, not a pointer to a point. A procedure that takes a class(point) dummy receives the address of that parent part and reads it where it is, so passing a large child costs no copy.

The child constructor

point3(1.0, 2.0, 2.0) takes the parent's components first and then the child's, in declaration order. The keyword form reads better and is worth the extra words when a child has many components: point3(x=1.0, y=2.0, z=2.0) is the same value. A parent component with a default may be left out, exactly as in a plain type.

For a child with allocatable components or an invariant to keep, a structure constructor is not enough, and the module offers a function that returns the constructed value, as section 072 did for make_series. The constructor is then private to the module and the function is the public name.

A CLASS() dummy

class(point), intent(in) :: p accepts a type(point) and also any type that extends it. Inside, only the components and bindings of point can be named: p%z is a compile error because the declared type does not have it, even when the actual argument does. That rule is what makes a class() argument safe to write once and call forever.

A type(point) dummy accepts a type(point) and nothing else. So the choice between the two declarations is the whole difference between a procedure that works on a family of types and one that works on exactly one, and a library that expects to grow should declare class() from the first day.

Inherited bindings

q%show(...) works because the child inherits the binding, and the passed-object dummy is declared class(point), which is why it accepts the child at all. The same declaration is what lets describe take either type: it is an ordinary subroutine whose dummy is polymorphic, and polymorphic is exactly what class() means.

A binding inherited this way is not virtual in the C++ sense: the name is bound to the parent procedure at compile time, and the call runs that procedure unless the child declares its own binding of the same name. With the child's binding present and the object passed as class(point), the call is resolved at run time, and that is lesson 076.

A parent where a child is expected

The direction matters. A child may be used where the parent is expected; a parent may not be used where the child is expected, because the child has a component the parent does not:

program badanarg implicit none type :: point real :: x = 0.0 real :: y = 0.0 end type point type, extends(point) :: point3 real :: z = 0.0 end type point3 type(point) :: p p = point(1.0, 2.0) call needs_z(p) contains subroutine needs_z(v) type(point3), intent(in) :: v print *, v%z end subroutine needs_z end program badanarg

gfortran -Wall -Wextra -std=f2018 e.f90 -o e

e.f90:16:17:

16 | call needs_z(p)
| 1
Error: Type mismatch in argument 'v' at (1); passed TYPE(point) to TYPE(point3)

The message names the dummy and both types, which is the clearest a type error gets. Declaring the dummy class(point) and testing for the child inside the procedure is the way to write a procedure that accepts both, and that test is select type.

When to extend

Extension is the right tool when a child really is a parent with more: a point with a third coordinate, a file-backed series of an in-memory one, a solver with a different stopping rule. It is the wrong tool when the two types merely share a few components, because the child then carries a parent it does not want and every procedure written for the parent becomes a procedure the child must tolerate.

A composition is often better: a type that has a point rather than is a point, with a component type(point) :: position. Composition has no inherited binding to argue about and no question of which constructor calls which. Fortran has both, and section 082 compares them for a small hierarchy.

Exercises

  1. Add a third type type, extends(point3) :: point4 with a component real :: w. How many components does point4 have, and in what order does its constructor take them?
  2. Call describe with a type(point3) and then read p%z inside it. What does the compiler say?
  3. Remove describe from the public list and rebuild the program. What happens to the two calls?
  4. Write a function to_point3(p) that takes a point and returns a point3 with z = 0. Is the reverse conversion possible without a test?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does type, extends(point) :: point3 declare?

    Answer

    A type with every component of point plus its own

    Extension adds components; the parent is unchanged.

  2. What does a CLASS(point) dummy accept?

    Answer

    A point and any type that extends it

    Polymorphic means the declared type or one of its extensions.

  3. What does this program print? module m implicit none type :: a integer :: n = 1 end type a type, extends(a) :: b integer :: m = 2 end type b end module m program p use m implicit none type(b) :: v v = b(7, 9) print *, v%n, v%m end program p

    Answer

    7 9

    The constructor takes the parent components first.

  4. Which binding does q%norm() run when point3 adds none?

    Answer

    The one inherited from point

    The child inherits the binding of the parent.

  5. Why is q%norm() 2.236 and not 3.0?

    Answer

    The parent binding sees only the parent components

    Inside a class(point) procedure only the parent part is visible.

  6. What does gfortran report when a point is passed to a type(point3) dummy?

    Answer

    Type mismatch in argument ...; passed TYPE(point) to TYPE(point3)

    The parent lacks the component the child promises.

  7. What may a child type NOT do to its parent?

    Answer

    Remove a parent component or hide a public binding

    A child only ever adds to what it inherits.

  8. How many components does a type have after two levels of extension?

    Answer

    All of them: the grandparent, the parent and its own

    Each level adds to what it inherits.

  9. When is composition better than extension?

    Answer

    When the types merely share components and the child is not a parent

    A HAS-A relationship is not an IS-A relationship.

  10. What does passing a child to a class(parent) dummy cost?

    Answer

    Nothing at run time: the parent part is read where it is

    The parent part is the first part of the child record.

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