Does the INTERFACE
statement in Fortran make it a programming language officially implementing multiple dispatch? (I ask because the Wikipedia article linked does not feature Fortran among its seemingly comprehensive listing of example programming languages supporting the paradigm in question).

- 57,977
- 4
- 76
- 119

- 67
- 4
-
The `interface` block has many independent uses in Fortran. Which one in particular so you have in mind? How do you want to use it to implement multiple dispatch? – Vladimir F Героям слава Jun 25 '22 at 18:09
-
@VladimirFГероямслава I've seen examples where naming the interface block allows you to call any function within the block by the interface name instead of the function name. In such a case, the compiler presumably determines which function within the block is being called by the number of arguments entered and their type instead of its name within the block. – wanderinganon Jun 25 '22 at 18:19
-
As I write in my answer, this is exactly the same like generics in C++, completely compile-time static dispatch. No multiple dispatch. Perhaps check https://lukasatkinson.de/2016/dynamic-vs-static-dispatch/ or some siimilar introducrory resource. – Vladimir F Героям слава Jun 25 '22 at 21:23
1 Answers
Interface blocks (introduced by the interface
keyword) are often used for generic interfaces. They work like C++ generics, no dynamic dispatch. You must distinguish static dispatch and dynamic dispatch. Both Fortran and C++ only have single dynamic dispatch by polymorphism using classes and inheritance/overloading.
But interface blocks themselves have several independent kinds of usage in Fortran and only some deal with some kind of overloading. Often they just work like a function declaration in a C++ header.
Take the example from https://www.geeksforgeeks.org/function-overloading-c/ :
void add(int a, int b)
{
cout << "sum = " << (a + b);
}
void add(double a, double b)
{
cout << endl << "sum = " << (a + b);
}
In Fortran you can do the same but instead declaring both subroutines with the same name straight away, you define two specific subroutines with a different name and make a generic interface for them
interface add
procedure add_ints
procedure add_doubles
end interface
...
subroutine add_ints(a, b)
integer :: a, b
print *, "sum = ", (a + b)
end subroutine
subroutine add_doubles(a, b)
double precision :: a, b
print *, "sum = ", (a + b)
end subroutine
This is the good old static dispatch.

- 57,977
- 4
- 76
- 119
-
So if a programming language allows for [function overloading](https://en.wikipedia.org/wiki/Function_overloading), the method of dispatch can either be _static_ (during compile-time) or _dynamic_ (during run-time) along with _single_ or _multiple_? In Fortran's case it allows for _static-multiple-dispatch_ via the `interface` statement and _dynamic-single-dispatch_ via its object orientate programming (OOP) facilities? I fail to see the strict resemblance between Fortran `interface`-ing and [C++ generic programming](https://en.wikipedia.org/wiki/Generic_programming#Templates_in_C++). – wanderinganon Jun 25 '22 at 23:39
-
1More or less yes. There is no Fortran `interface`ing as such, interface blocks serve several purposes. Fortran generics defined using *generic (named) interface blocks* are the same as static function overloading in C++. C++ generic programming is a much broader topic and includes templates and metaprogrmming - neither of which are available in Fortran. Some C++ programmers will also use templates where ordinary function overloading suffices, but what I was refering to was the ordinary function overloading only, no templates. – Vladimir F Героям слава Jun 26 '22 at 05:26
-
I think I fully understand now. The article linked in my question ([multiple dispatch](https://en.wikipedia.org/wiki/Multiple_dispatch)) refers to the special case where "dispatching" the generic function (_dynamically_) occurs during run-time. And because Fortran achieves this (_statically_) only during compile time it doesn't appear listed in the article as an example. So a better title of that Wikipedia entry would've been _dynamic dispatch_ with with the inclusion of the _single dispatch_ case in a subsection apart from _multiple dispatch_. – wanderinganon Jun 27 '22 at 10:54