Re: dynamic type in function calls?

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.

-- glen


Relevant Pages

  • Re: Reading in a binary file that contains floats and integers
    ... your comment indicated that you considerd the loop ... and get the dirty work done in the getRecord function. ... info at compile time. ... I'd like to see that done in fortran. ...
  • Re: About alternatives to Matlab
    ... But whether a single loop is faster than several BLAS calls does not ... Bad Fortran 95 can easily be 25% lower than well-tuned C99. ... NumPy can be slower than equivalent C by an order of magnitude. ... manipulated within Python, not the speed of python code using NumPy ...
  • Re: Coding in Fortran
    ... I would like to start some programming in fortran. ... a loop got performed at least once. ... That f77 feature was one of the last to be implemented uniformly. ...
  • Re: should every thing be zero indexed?
    ... Jim Rogers wrote: ... >> This for loop would have the structure of a quantification: ... >> as, arrays. ... > I acknowledge that the concept of arrays was used heavily in Fortran, ...
  • Re: Letter to US Sen. Byron Dorgan re unpaid overtime
    ... >> both less efficient and less safe than the Fortran and Basic standard. ... >> The C for loop is actually trying to do what a do loop does. ... sloppy thinking that results from confusing a programming language ... > I do not believe that you are capable of writing a conforming C compiler. ...