Fortran: parameterized derived types in a select type clause

I am trying to use parameterized derived types in a routine using an unlimited polymorphic pointer.

Can I use the select type clause for parameterized types?

I tried something in the following lines, but getting a compilation error. (Syntax error in or near TYPE)

module mod_real implicit none type :: type1(k) integer, kind :: k = 4 real(kind=k) :: val end type type1 contains subroutine out(in) class(*) :: in select type(in) type is (type1(4)) print *, 'real(4):', in%val type is (type1(8)) print *, 'real(8):', in%val end select end subroutine out end module mod_real program real_test use mod_real type(type1(4)) :: p type(type1(8)) :: p2 p%val = 3.14 p2%val = 3.1456d0 call out(p) call out(p2) end program real_test 

strings with "type is (type1 (4))" and "type is (type1 (8))" are indicated as having the wrong syntax. I am using the Fortran Group Fortran compiler (version 13.5-0).

+6
source share
1 answer

At the time of writing the question, the problem was most likely the compiler support, check this page:

As for the actual question, in this case you can use the solution for compiling with the module procedure , which does not need polymorphism and, therefore, may have less overhead:

 module mod_real type type1(k) ... ! as before end type interface out module procedure out4, out8 end interface contains subroutine out_type4(x) type(type1(4)), intent(in) :: x print*, 'real(4):' x%val end subroutine subroutine out_type8(x) type(type1(8)), intent(in) :: x print*, 'real(8):' x%val end subroutine end module program ... ! as before end program 
+1
source

Source: https://habr.com/ru/post/969385/


All Articles