fortranmultiple-dispatch

Is Fortran a multiple dispatch programming language?


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).


Solution

  • 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.