Fortran: parameterized derived types in a 'select type' clause
Asked Answered
A

1

7

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

Is is possible to use a 'select type' clause for parameterized types?

I've tried something along the following lines but am getting a compilation error. (Syntax error at 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 

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

Auklet answered 16/5, 2014 at 15:2 Comment(3)
looks OK, which version of PGI Fortran are you using?Congeal
I'm using version 13.5-0Auklet
Note, most compilers did not implement parametrized derived types completely yet. Maybe also your compiler has only partial support for it.Gulfweed
T
1

At the time of the question's writing, the issue was most probably compiler support, check this page:

As to the actual question, in this case you could use a compile-time solution with module procedure, which wouldn't need polymorphism and thus might 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
Tigress answered 20/8, 2014 at 9:35 Comment(1)
At the time of writing the question PGI already claimed FULL Fortran 2003 compliance. You did not answer whether it is possible to use parametrized derived types in select typeSimplify

© 2022 - 2024 — McMap. All rights reserved.