Fortran Operator Overloading
Generic interfaces can also be attached to operators. You can give +, *, == and friends a meaning for your own derived types, define brand-new operators such as .dot., and customise assignment. Done well, this lets numerical code read like the mathematics it implements.
Overloading + and *
Use interface operator(+) and list the functions that implement it. A binary operator needs a function with two intent(in) arguments:
module vec3_mod
implicit none
private
public :: vec3, operator(+), operator(-), operator(*)
type :: vec3
real :: x = 0.0, y = 0.0, z = 0.0
end type vec3
interface operator(+)
module procedure add_vec
end interface
interface operator(-)
module procedure sub_vec, negate_vec ! binary and unary minus
end interface
interface operator(*)
module procedure scale_left, scale_right ! real * vec and vec * real
end interface
contains
pure function add_vec(a, b) result(c)
type(vec3), intent(in) :: a, b
type(vec3) :: c
c = vec3(a%x + b%x, a%y + b%y, a%z + b%z)
end function add_vec
pure function sub_vec(a, b) result(c)
type(vec3), intent(in) :: a, b
type(vec3) :: c
c = vec3(a%x - b%x, a%y - b%y, a%z - b%z)
end function sub_vec
pure function negate_vec(a) result(c)
type(vec3), intent(in) :: a
type(vec3) :: c
c = vec3(-a%x, -a%y, -a%z)
end function negate_vec
pure function scale_left(s, a) result(c)
real, intent(in) :: s
type(vec3), intent(in) :: a
type(vec3) :: c
c = vec3(s * a%x, s * a%y, s * a%z)
end function scale_left
pure function scale_right(a, s) result(c)
type(vec3), intent(in) :: a
real, intent(in) :: s
type(vec3) :: c
c = scale_left(s, a)
end function scale_right
end module vec3_mod
program vectors
use vec3_mod
implicit none
type(vec3) :: p, v, q
real :: dt = 0.5
p = vec3(1.0, 2.0, 3.0)
v = vec3(4.0, 0.0, -2.0)
q = p + v * dt ! reads like the physics: x + v*dt
print '(3f7.2)', q
print '(3f7.2)', 2.0 * p - v
print '(3f7.2)', -q
end program vectors 3.00 2.00 2.00
-2.00 4.00 8.00
-3.00 -2.00 -2.00
Overloaded operators keep their normal precedence: p + v * dt multiplies first, exactly as with numbers.
Defined operators: .dot. and .cross.
You can invent operators whose names are letters between dots. User-defined unary operators bind tightest, binary ones loosest (lower than any intrinsic operator), so use parentheses when mixing them:
module vector_ops
implicit none
private
public :: operator(.dot.), operator(.cross.), operator(.norm.)
interface operator(.dot.)
module procedure dot3
end interface
interface operator(.cross.)
module procedure cross3
end interface
interface operator(.norm.)
module procedure norm3
end interface
contains
pure function dot3(a, b) result(d)
real, intent(in) :: a(3), b(3)
real :: d
d = sum(a * b)
end function dot3
pure function cross3(a, b) result(c)
real, intent(in) :: a(3), b(3)
real :: c(3)
c = [a(2)*b(3) - a(3)*b(2), a(3)*b(1) - a(1)*b(3), a(1)*b(2) - a(2)*b(1)]
end function cross3
pure function norm3(a) result(n)
real, intent(in) :: a(3)
real :: n
n = sqrt(sum(a**2))
end function norm3
end module vector_ops
program use_ops
use vector_ops
implicit none
real :: ex(3) = [1.0, 0.0, 0.0], ey(3) = [0.0, 1.0, 0.0]
real :: a(3) = [3.0, 4.0, 12.0]
print '(a, f6.2)', 'ex . ey = ', ex .dot. ey
print '(a, 3f6.2)', 'ex x ey = ', ex .cross. ey
print '(a, f6.2)', '|a| = ', .norm. a
print '(a, f6.2)', 'a . (ex+ey) = ', a .dot. (ex + ey)
end program use_opsex . ey = 0.00
ex x ey = 0.00 0.00 1.00
|a| = 13.00
a . (ex+ey) = 7.00
Note that these operators work on plain arrays, not only on derived types: any operand types are fine for a new operator. (For intrinsic operators such as + you can't redefine what they do on intrinsic types.)
Overloading comparisons
Comparison operators return a logical. Here a money type stores integer cents so comparisons and sums are exact:
module money_mod
implicit none
private
public :: money, operator(+), operator(<), operator(==), show
type :: money
integer :: cents = 0
end type money
interface operator(+)
module procedure add_money
end interface
interface operator(<)
module procedure less_money
end interface
interface operator(==)
module procedure equal_money
end interface
contains
elemental function add_money(a, b) result(c)
type(money), intent(in) :: a, b
type(money) :: c
c%cents = a%cents + b%cents
end function add_money
elemental logical function less_money(a, b)
type(money), intent(in) :: a, b
less_money = a%cents < b%cents
end function less_money
elemental logical function equal_money(a, b)
type(money), intent(in) :: a, b
equal_money = a%cents == b%cents
end function equal_money
function show(m) result(s)
type(money), intent(in) :: m
character(len=:), allocatable :: s
character(len=20) :: buf
write(buf, '(i0, ".", i2.2)') m%cents / 100, mod(m%cents, 100)
s = '