Generic interfaces
One name can stand for several procedures, and the call picks the one whose arguments fit. Fortran calls the name a generic and the procedures behind it specifics; the mechanism is the one behind abs and sum, written by the language itself. This lesson builds a generic of three specifics, resolves a call by the argument type, and reads the error two identical specifics produce.
A generic name
interface show with module procedure show_int, show_real, show_str declares a generic called show whose specifics are three module procedures. The interface sits in the module, before contains, and the specifics follow it.
The file generic.f90:
module convert
implicit none
interface show
module procedure show_int, show_real, show_str
end interface show
contains
subroutine show_int(x)
integer, intent(in) :: x
print '(a,i0)', "integer : ", x
end subroutine show_int
subroutine show_real(x)
real, intent(in) :: x
print '(a,f8.2)', "real : ", x
end subroutine show_real
subroutine show_str(x)
character(len=*), intent(in) :: x
print '(a,a)', "string : ", trim(x)
end subroutine show_str
end module convert
program generic_demo
use convert, only: show
implicit none
call show(42)
call show(3.5)
call show("hello")
end program generic_demo
gfortran -Wall -Wextra -std=f2018 generic.f90 -o generic
./generic
integer : 42
real : 3.50
string : hello
Three calls of one name reached three procedures. The program never names a specific, and the specifics can be renamed, added or reordered without a change in any caller: the module is the only place that knows which procedure a type goes to.
How the call is resolved
The compiler matches the call against the specifics and takes the one whose argument types, kinds and ranks fit exactly or by a standard conversion it is allowed to make. An exact match wins; a call that fits two specifics equally well is ambiguous and is an error, which is the subject of the last section.
A generic may mix functions and subroutines only as long as a call can tell them apart, and in practice a generic name is one kind. The name of the generic is what a caller uses with use ... only; the specifics stay private and do not appear in the caller at all.
A generic function
Functions work the same way, and the result type may differ between specifics. A generic norm with an integer and a real version returns a real either way, and a call reads like a single mathematical function.
module norms
implicit none
interface norm
module procedure norm_int, norm_real
end interface norm
contains
function norm_int(v) result(r)
integer, intent(in) :: v(:)
real :: r
r = sqrt(real(sum(v * v)))
end function norm_int
function norm_real(v) result(r)
real, intent(in) :: v(:)
real :: r
r = sqrt(sum(v * v))
end function norm_real
end module norms
program norm_demo
use norms, only: norm
implicit none
integer :: a(3)
real :: b(3)
a = [1, 2, 2]
b = [1.0, 2.0, 2.0]
print '(a,f8.3)', "integer : ", norm(a)
print '(a,f8.3)', "real : ", norm(b)
end program norm_demo
gfortran -Wall -Wextra -std=f2018 norm.f90 -o norm
./norm
integer : 3.000
real : 3.000
Both calls print three, because the integer version converts its sum to a real before the square root and the real version does not need the conversion. The two specifics exist because an integer array cannot be passed to a real dummy without a copy, and the generic hides that choice from the caller.
The ambiguity error
Two specifics with the same argument types make the generic unusable, and the compiler says so at the declaration rather than at a call:
program ambiguous
implicit none
interface g
procedure :: a, b
end interface g
call g(1)
contains
subroutine a(x)
integer, intent(in) :: x
print *, x
end subroutine a
subroutine b(y)
integer, intent(in) :: y
print *, y
end subroutine b
end program ambiguous
gfortran -Wall -Wextra -std=f2018 ambiguous.f90 -o ambiguous
ambiguous.f90:9:3:
9 | subroutine a(x)
| 1~~~~~~~~~~~
......
14 | subroutine b(y)
| 2~~~~~~~~~~~
Error: Ambiguous interfaces in generic interface 'g' for 'a' at (1) and 'b' at (2)
The two procedures take the same type, so no call can choose between them. The fix is a different type or rank in one of the specifics, and the error names both so the choice is easy.
When a generic is worth it
A generic is worth its lines when the same operation is natural for several types and the caller should not think about which one it has: printing a value, taking a norm, writing an array of numbers to a file. It is not worth it for two procedures that do different things, because a shared name suggests a shared meaning.
The other use is the one the next lesson takes up: a generic named after an operator, so that + or == works for a derived type. The mechanism is the same interface, with an operator name instead of an identifier.
Exercises
- Add a fourth specific to show for a logical value. Which call reaches it?
- Call show with a double-precision literal such as 3.5d0. Does the real specific accept it, and what happens?
- Make norm work on a matrix by adding a rank-two specific. Which call is then ambiguous?
- Remove one of the two specifics from the ambiguous program and call the generic. What does the output look like?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is a generic interface?
Answer
One name for several specific procedures, chosen by the arguments
The call picks the specific that fits.
-
What does
module procedurelist inside an interface?Answer
The specifics that share the generic name
Each name is a module procedure behind the generic.
-
What does this program print?
module m implicit none interface pick module procedure pick_i, pick_r end interface pick contains subroutine pick_i(x) integer, intent(in) :: x print *, "int", x end subroutine pick_i subroutine pick_r(x) real, intent(in) :: x print *, "real", x end subroutine pick_r end module m program p use m implicit none call pick(2) end program pAnswer
int 2
The integer call reaches the integer specific.
-
What happens when two specifics accept exactly the same argument types?
Answer
The compiler reports an ambiguous interface
No call could choose between them.
-
Why does a generic hide the specifics from the caller?
Answer
The caller uses the generic name, so the specifics can change inside the module
The module is the only place that maps a type to a procedure.
-
Which intrinsic is itself a generic?
Answer
abs
abs accepts integers, reals and complexes.
-
When is a generic NOT worth writing?
Answer
When the procedures do different things and the shared name would mislead
A shared name suggests a shared meaning.
-
What may differ between two specifics of one generic?
Answer
The types, kinds and ranks of their arguments
The difference is what lets a call be resolved.
-
Where is the interface of a generic written?
Answer
In the module that holds the specifics, before CONTAINS
The module owns the generic and its specifics.
-
What does the next lesson do with the same mechanism?
Answer
Gives an operator such as + a meaning for a derived type
Operator overloading is a generic interface with an operator name.
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