Alias for the name of a type?

Is it possible to define an alias for the name of a type? For example, is it possible to define my_real as an alias for real(kind(0.0D0))? Thank you.

The only way I know would be to use a preprocessor to expand the alias. What’s your goal? Depending on what you want to accomplish, there is likely a better solution.

1 Like

Thank you @nshaffer !

My motivation is to localize the usage of preprocessors and header files. More precisely, I am writing MEX gateways for interfacing a Fortran library with MATLAB. This necessitates the usage of fintrf.h defined by MathWorks. This header file defines many types (e.g., mwSize, mwIndex, mwPointer, …). I hope to compose a module (e.g., fintrf_mod) that includes fintrf.h and defines aliases for these types, so that I can use fintrf_mod instead of including fintrf.h and use the aliases instead of the MathWorks-defined types. In this way, I will only need to preprocess that code of fintrf_mod but nothing else.

Many thanks!

An alternative is to define a derived type and fill that with a single component of the right type and kind. That will hide the details. The price you pay is that you have to use something like:

type(mwSize) :: mysize

call something( mysize%data, ... )

Or something similar.

1 Like

Is something like the typedef specifier in C++ necessary in Fortran?

Some of C++'s fundamental types like unsigned long long are verbose, but Fortran doesn’t seem to have this issue. integer(8) looks fine.

I feel like the typedef specifier encourages people to give all kinds of weird names to C++'s fundamental types, which I don’t like.

1 Like

Not exactly what you are asking for, but you can name the integer KIND in a module

module kind_mod
integer, parameter :: double_prec = kind(1.0d0)
end module kind_mod

and then use that kind in the program

program main
use kind_mod, only: double_prec
real(double_prec) :: x
end program main
1 Like

Thank you all, @nshaffer, @Arjen, @fortran4r, and @Beliavsky, for your inputs and insights. According to the discussion, it seems that there does not exist a straightforward way of really defining an alias for the name of a particular type. So I will tolerate the inclusion of the MathWork’s header file wherever it is needed.

Many thanks again!