Re: dynamic type in function calls?
- From: glen herrmannsfeldt <gah@xxxxxxxxxxxxxxxx>
- Date: Tue, 5 Apr 2011 17:36:51 +0000 (UTC)
Paul Anton Letnes <paul.anton.letnes@xxxxxxxxx> wrote:
No, not really. I was hoping there was some magic that would
'auto-write' square_s and square_d so that you would not have to
manually type in 2 different functions. It's the
two-function-implementation I would like to avoid.
I believe C++ does this with what is called templates:
As explained in the referenced page, templates, like #define,
are expanded at compile time. As with C, that is sometimes done
using the C preprocessor and #define with Fortran.
The usual system used with Fortran is generic, which allows for
selection of one of a set of different procedures based on
the type, rank, and kind of the arguments. (Selection between
different compiled versions.) According to the wikipedia page
Generic_programming, a similar system exists in Ada, Eiffel, Java,
C#, and Visual Basic .NET. That page seems to call the C++
from parametric polymorphism.
I believe Fortran (as of F2003 or F2008) allows for the ability
to pass pointers to unknown types, similar to C (void*) or
Java's Object reference variables, through a procedure to a
called procedure that does know about the actual type.
That allows, for example, the ability to write a generic
sort routine that will sort objects of any type.
Compile time expansion could probably also be done with preprocessor
procedures in PL/I. The old favorite example would do loop
unrolling for small (constant) loop sizes, and generate an actual
loop for larger sizes.
Note that in some cases the actual algorithm needed will change,
which complicates the expansion.