Operator overloading and ASSIGNMENT(=)

A derived type does not know what + means, and the compiler refuses an expression that asks it. The generic interface of the previous lesson can give an operator a meaning for a type: the same mechanism, with a name that is a symbol. This lesson defines arithmetic for a two-dimensional vector, both operators and an assignment, and reads the error the type gives when nothing is defined.

INTERFACE OPERATOR

interface operator(+) with module procedure add_vec gives + a meaning for the arguments of add_vec. Inside the module the type and the operator are declared together, so every user of the module gets both.

The file vector.f90:

module vecmod implicit none type :: vec real :: x, y end type vec interface operator(+) module procedure add_vec end interface operator(+) interface operator(*) module procedure scale_vec, scale_left end interface operator(*) interface assignment(=) module procedure assign_real end interface assignment(=) contains function add_vec(a, b) result(c) type(vec), intent(in) :: a, b type(vec) :: c c%x = a%x + b%x c%y = a%y + b%y end function add_vec function scale_vec(a, k) result(c) type(vec), intent(in) :: a real, intent(in) :: k type(vec) :: c c%x = a%x * k c%y = a%y * k end function scale_vec function scale_left(k, a) result(c) real, intent(in) :: k type(vec), intent(in) :: a type(vec) :: c c = scale_vec(a, k) end function scale_left subroutine assign_real(a, v) type(vec), intent(out) :: a real, intent(in) :: v a%x = v a%y = 0.0 end subroutine assign_real end module vecmod program vector_demo use vecmod implicit none type(vec) :: p, q, r p = vec(1.0, 2.0) q = vec(3.0, 4.0) r = p + q print '(a,2f6.1)', "p + q = ", r%x, r%y r = p * 2.0 print '(a,2f6.1)', "p * 2 = ", r%x, r%y r = 3.0 * p print '(a,2f6.1)', "3 * p = ", r%x, r%y p = 5.0 print '(a,2f6.1)', "p = 5.0 = ", p%x, p%y end program vector_demo

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

p + q = 4.0 6.0
p * 2 = 2.0 4.0
3 * p = 3.0 6.0
p = 5.0 = 5.0 0.0

Four lines, each one an operator that Fortran does not define for the type: the module does. The constructor vec(1.0, 2.0) is the structure constructor of the derived type, the % reads a component, and the arithmetic is what the four specifics implement.

Both orders of an operand

p * 2.0 and 2.0 * p are different calls: the first has the type on the left and the real on the right, the second the other way round. One specific cannot serve both, so the interface lists two, scale_vec with the order (vec, real) and scale_left with (real, vec). The second is a four-line wrapper that calls the first.

That pair is the whole trick of operator overloading for a scalar factor, and it is worth writing both even when the program only uses one: a reader who writes 2.0 * p should not have to learn that the type only supports the other order.

ASSIGNMENT(=)

interface assignment(=) gives the equals sign a meaning for a pair of types. Here a real assigned to a vec sets the x component and leaves y at zero, so p = 5.0 is a conversion written as an assignment.

The specific has the target as its first dummy with intent(out) and the value as the second, and it may not be a function. The operator is the most dangerous of the overloaded forms: an assignment that silently changes a value is the one a reader is least likely to check, so the meaning should be a conversion the type promises everywhere, never a side effect.

A type with no operator

Without the interface the expression is a compile error, and the message says exactly what is missing:

program noover implicit none type :: vec real :: x, y end type vec type(vec) :: p, q, r p = vec(1.0, 2.0) q = vec(3.0, 4.0) r = p + q print *, r%x, r%y end program noover

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

noover.f90:12:6:

12 | r = p + q
| 1
Error: Unexpected derived-type entities in binary intrinsic numeric operator '+' at (1)

The phrase "unexpected derived-type entities" is the compiler saying that it knows the operator and does not know it for this type. The fix is the interface of the first section and nothing else.

Overload with a meaning

An operator should mean what it means for a number. + combines two values of the type into one of the same type, * scales or composes, == compares and returns a logical. An operator that writes a file, prints or changes its arguments breaks the arithmetic a reader assumes and is the reason some projects forbid overloading altogether.

The safe rules are three: every specific is a function with intent(in) arguments and a result of the type, the operator has an identity and an inverse if it claims one, and both operand orders exist when a scalar may sit on either side. The rest is a matter of taste, and the taste of the language is that an operator is a function.

Exercises

  1. Add operator(-) for two vectors. Which specific do you write, and what does it return?
  2. Define operator(.dot.) as the inner product of two vectors. Is the dot form a valid operator name?
  3. Remove scale_left from the interface and run the program. Which line stops compiling?
  4. Give assignment(=) a second specific that converts a vec to a real. Which assignment then becomes legal?

Questions

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

  1. What does interface operator(+) do?

    Answer

    Gives the plus sign a meaning for the arguments of the listed procedures

    The interface is a generic whose name is an operator.

  2. Why does the vector module list two specifics for *?

    Answer

    vec * real and real * vec are two different argument lists

    One specific cannot match both orders.

  3. What does this program print? module m implicit none type :: pair integer :: a, b end type pair interface operator(+) module procedure add end interface operator(+) contains function add(x, y) result(z) type(pair), intent(in) :: x, y type(pair) :: z z%a = x%a + y%a z%b = x%b + y%b end function add end module m program p use m implicit none type(pair) :: x, y, z x = pair(1, 2) y = pair(10, 20) z = x + y print *, z%a end program p

    Answer

    11

    The a components add to eleven.

  4. What does interface assignment(=) define?

    Answer

    A meaning for the equals sign between two types

    The specific takes the target and the value.

  5. What must the specific of assignment(=) be?

    Answer

    A subroutine with the target as its first dummy

    An assignment has no result variable.

  6. What does gfortran report for p + q on a type with no operator(+)

    Answer

    Error: Unexpected derived-type entities in binary intrinsic numeric operator

    The operator exists but not for that type.

  7. Which operator name is valid in Fortran?

    Answer

    .dot.

    A user-defined operator is written between dots.

  8. What should an overloaded + never do?

    Answer

    Print, read a file or change its arguments

    An operator is arithmetic and a reader assumes no side effects.

  9. Why is the assignment operator the most dangerous of the overloads?

    Answer

    A reader is least likely to check an assignment for a hidden conversion

    The equals sign usually means a plain copy.

  10. Which are the arguments of a specific for operator(*)?

    Answer

    The two operands, in the order they appear

    An operator is a function of its two operands.

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