Generic programming is a method for reusing a single piece in different contexts, such as with multiple data types. For example, the main body of a single numerical procedure can be reused for both single and double precision real variables or the code for a linked list can be reused for lists of integers, reals, or strings.
Generic programming is not available in Fortran 95 and it is only available in the form of parametrized user-defined types in Fortran 2003. However, the use of a preprocessor can assist with generic programming.
Some kinds of generic programming can be achieved using the transfer intrinsic, which copies bit patterns from one datatype to another without casting the value. Using this process, many data structures such as lists, stacks, queues, etc can be implemented by making the routines accept arrays of characters, and then using the transfer intrinsic to convert values before and after they are stored in the structure.
Here is an example of using the transfer intrinsic function to achieve simple generic data storage. The following code compiles with gfortran 4.6:
module data_mod
implicit none
private
integer,parameter::N = 10
character,dimension(1),parameter::void = [achar(0)]
type::data_t
character,dimension(:),allocatable::d
end type
type(data_t),dimension(N)::data
public::void
public::set,get
contains
subroutine set(k,d)
integer,intent(in)::k
character,dimension(:),intent(in)::d
if(allocated(data(k)%d)) deallocate(data(k)%d)
allocate(data(k)%d(size(d)))
data(k)%d = d
end subroutine set
function get(k) result(d)
integer,intent(in)::k
character,dimension(:),allocatable::d
allocate(d(size(data(k)%d)))
d = data(k)%d
end function get
end module data_mod
program main
use data_mod
implicit none
call set(1,transfer(10,void))
call set(2,transfer(10.0,void))
call set(3,transfer(10.0d0,void))
write(*,*) transfer(get(1),0)
write(*,*) transfer(get(2),0.0)
write(*,*) transfer(get(3),0.0d0)
end program main
The output of the above code is as follows
10
10.000000
10.000000000000000
See McGavin and Young (2001) and FLIBS for generic linked list implementations.
James Van Buskirk has written a generic version of BLAS available here: http://home.comcast.net/~kmbtib/Fortran_stuff/GENERIC_BLAS.ZIP
Donev, Aleksandar (2004). Genericity in extending Fortran. ACM SIGPLAN Fortran Forum 23(1), 2-13.
Markus, A. (2001). Generic programming in Fortran 90. ACM SIGPLAN Fortran Forum 20(3), 20-23.
McCormack, D. (2005). Generic programming in Fortran with Forpedo. ACM SIGPLAN Fortran Forum 24(2), 18-29.
McGavin, P. and R. Young (2001). A generic list implementation. ACM SIGPLAN Fortran Forum 20(1), 16-20.