Is it possible to identify and distinguish between types with Fortran Select Type?
2 answers
Yes, you can write something like the following. Here I am using real view constants from the built-in module iso_fortran_env
.
SELECT TYPE(areal)
TYPE is (REAL(real32))
WRITE(*,*) '... real32'
TYPE is (REAL(real64))
WRITE(*,*) '... real64'
CLASS default
WRITE(*,*) '... default'
END SELECT
Please note that you cannot write
SELECT TYPE(areal)
TYPE is (REAL(real32))
WRITE(*,*) '... real32'
TYPE is (REAL(real64))
WRITE(*,*) '... real64'
TYPE is (REAL)
WRITE(*,*) '... real'
CLASS default
WRITE(*,*) '... default'
END SELECT
In this case, the real default type (probably real32
for most modern compilers) will correspond to two statements of type guard, as well as the error that the compiler should receive.
+1
source to share