Fortran 子例程可以使用多态性来做这样的事情吗:
subroutine (inarr)
class(*) :: inarr
select type(inarr)
type is (real, allocatable(:))
! do something for 1-D array, maybe even allocate it
type is (real, allocatable(:,:))
! do something for 2-D array, maybe even allocate it
type is etc.
上面的语法当然行不通。但我想我想做的事情很明确。有什么办法可以做到吗?
也许你想要的是假定的排名特征:
subroutine (inarr)
real, allocatabe :: inarr(..) ! .. denote any possible rank
select rank(inarr)
rank(1)
! do something for 1-D array, maybe even allocate it
allocate( arr(n) )
! ...
rank(2)
! do something for 2-D array, maybe even allocate it
allocate( arr(n,m) )
! ...
end select