Clicky

Fortran Wiki
Forpedo

Forpedo is a preprocessor for Fortran 90 code, written in Python. It performs some typical preprocessing tasks, such as including code from one file in another, but it is mostly designed to assist with advanced techniques in pre-Fortran 2003 versions of the language. In particular, there is support for C++ like generics (templates), and run-time polymorphism from Object-oriented programming.

The output of Forpedo is standard Fortran code. The output generated is clean, as if it were written by a programmer, so that if at some point you stop using Forpedo, you will be left with a high-quality body of code.

Forpedo was announced on November 24, 2004. Support for run-time polymorphism was announced on February 16, 2006.

Homepage: http://www.macanics.net/forpedo/

Run-Time Polymorphism

To define a polymorphic type (known as a ‘protocol’) with Forpedo, you can do something like this:

#protocol AnimalProtocol AnimalProtocolMod

#method makeSound
type(AnimalProtocol), intent(in) :: self
#endmethod

#method increaseAgeInAnimalYears increase
type(AnimalProtocol), intent(inout) :: self
integer, intent(in)                 :: increase
#endmethod

#conformingtype Dog DogMod
#conformingtype Cat CatMod

#endprotocol

To use the type:

program Main
  use AnimalProtocolMod
  use DogMod
  use CatMod
  type (Dog), target    :: d
  type (Cat), target    :: c
  type (AnimalProtocol) :: p

  ! Assign protocol to Dog
  p = d

  ! Pass pointer to a subroutine that knows nothing about the concrete type Dog
  call doStuffWithAnimal(p)

  ! Repeat for Cat. Results will be different, though subroutine call is the same.
  p = c
  call doStuffWithAnimal(p)

contains

  subroutine doStuffWithAnimal(a)
    type (AnimalProtocol) :: a
    call makeSound(a)
    call increaseAgeInAnimalYears(a, 2)
  end subroutine

end program

See Also

References