我试图在子程序中使用无限多态指针使用参数化派生类型。
是否可以对参数化类型使用“选择类型”子句?
我已经尝试了以下方法,但是遇到编译错误。
(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
带有“type is(type1(4))”和“type is(type1(8))”的行表示语法不正确。我正在使用Portland Group Fortran编译器(版本13.5-0)。
最佳答案
在撰写问题时,问题很可能是编译器支持,请检查此页面:
至于实际的问题,在这种情况下,您可以使用
module procedure
的编译时解决方案,该解决方案不需要多态性,因此可能会减少开销: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
关于types - Fortran: 'select type'子句中的参数化派生类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23698561/