Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fortran: parameterized derived types in a 'select type' clause

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).

like image 909
AlRi Avatar asked May 16 '14 15:05

AlRi


1 Answers

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

  • http://fortranwiki.org/fortran/show/Fortran+2003+status

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
like image 143
Markus Avatar answered Nov 15 '22 23:11

Markus