Operator overloading

Contents
Interface operator
Interface assignment
Define + for a type
Overload sparingly

Interface operator

An interface operator block tells the compiler that a familiar symbol such as plus also accepts a user type.

Inside the block, name the module procedures that implement the operation for each combination of argument types.

The block below connects the plus sign with a function that adds two points.

interface operator(+) module procedure point_add end interface

Interface assignment

An interface assignment block extends the equals sign so that assignment between user types can run custom logic.

The implementing subroutine takes two arguments: the left side with intent out or inout, and the right side with intent in.

This block registers a copy routine for the point type.

interface assignment(=) module procedure point_copy end interface

Define + for a type

The implementing function takes two values with intent in and returns the combined value through the result clause.

Keep the meaning close to the built-in operator: plus should add, not multiply or print, so readers are never surprised.

This module adds points component by component and exposes the operator to client code.

module point_ops implicit none type :: point real :: x real :: y end type point interface operator(+) module procedure point_add end interface contains function point_add(a, b) result(c) type(point), intent(in) :: a, b type(point) :: c c%x = a%x + b%x c%y = a%y + b%y end function point_add end module point_ops

Overload sparingly

Overloading makes code read well only when the new meaning matches the old one, so limit it to natural cases like math vectors.

Avoid clever symbols for unrelated actions, and never change the number of operands or the precedence of an operator.

When the operation has no clear symbol, a named function or type-bound procedure communicates the idea better than an overload.

program demo implicit none use point_ops type(point) :: a, b, c a = point(1.0, 2.0) b = point(3.0, 4.0) c = a + b print *, c end program demo

Next: Finalization

Article author: Arthur Isaev

Related articles
Fortran
Fortran course: zero to hero in 50 lessons
What is Fortran and where it runs
Install gfortran
Your first program
Program structure
Types: integer real complex logical character
Kind parameters
Operators
Conditions
DO loops
Arrays
Array sections
Character strings
Formatted IO
Files
Subroutines and functions
Modules
USE with ONLY
INTENT
OPTIONAL and PRESENT
Explicit interfaces
Derived types
Type-bound procedures
Object orientation
Operator overloading
Finalization
Allocatable arrays
Pointers in Fortran
DO CONCURRENT
Coarrays
OpenMP basics
Namelists
IEEE arithmetic
Intrinsic matrix math
Random numbers
String tools
Command-line arguments
Environment variables
error stop
Testing
Build with gfortran and make
Debugging
C interoperability
From fixed form to free form
Numeric models
Performance
Modern Fortran tour: 2008, 2018, 2023
Capstone: stats tool
Capstone: 1D heat equation
Style guide
Hero roadmap: the whole course on one page
Install gfortran on Windows 11
Install gfortran on Ubuntu
Install gfortran on Rocky Linux
Install gfortran on macOS
Install gfortran on FreeBSD
Fortran in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: