Oop 在Fortran扩展类型中指定多态组件
我正在编写一个模块,它定义了两个派生类型,每个派生类型都有一个具有公共父类型的派生类型组件,如下所示Oop 在Fortran扩展类型中指定多态组件,oop,fortran,derived-types,Oop,Fortran,Derived Types,我正在编写一个模块,它定义了两个派生类型,每个派生类型都有一个具有公共父类型的派生类型组件,如下所示 type :: aux0 integer :: a end type aux0 type, extends(aux0) :: aux1 integer :: b end type aux1 type, extends(aux0) :: aux2 integer :: c end type aux2 我想定义两个派生类型
type :: aux0
integer :: a
end type aux0
type, extends(aux0) :: aux1
integer :: b
end type aux1
type, extends(aux0) :: aux2
integer :: c
end type aux2
我想定义两个派生类型,每个派生类型分别具有aux1
和aux2
类型的组件。我有几个例程仅基于字段aux%a
(例如fun1
)执行某些工作。我想将这些方法绑定到cplx1
,cplx2
。因此,我为cplx1
,cplx2
创建了一个公共父级,其中包含类aux0
的字段aux
,并为公共函数编写了类aux0
变量接口。但是,我想在实际类型cplx1
,cplx2
中指定aux
组件的类型,因为一些其他函数需要字段aux
的特定类型。我想知道这是如何或是否可行的
module type
! ... aux# types definitions
type :: cplx0
class(aux0), allocatable :: aux(:)
contains
! routines that use aux % a
procedure, pass :: fun1
end type cplx0
type, extends(cplx0) :: cplx1
! type(aux1) :: aux(:) ! doesn't work
contains
! routines that use aux % b
end type cplx1
type, extends(cplx0) :: cplx2
! type(aux2) :: aux(:)! doesn't work
contains
! routines that use aux % c
end type cplx2
contains
function fun1(self)
class(cplx0) :: self
integer :: i
do i = 1, size(self % aux)
print *, self % aux(i) % a
end do
end function fun1
! ... more functions
end module type
如果我取消注释type(aux1)
,则错误为
Error: Component ‘aux’ at (1) already in the parent type at (2)
这是可以理解的,但我想知道如何绕过它。这是不可能的。如果希望通过组件的类型应用约束(基于在某种扩展层次结构中保存组件的类型),则需要在扩展中定义组件 给定本文中的示例代码,不需要将
fun1
中的逻辑绑定到cplx类型层次结构(看起来不像是cplx层次结构中的扩展将覆盖的过程)。fun1
中的逻辑可以在非类型绑定过程中,使用类型为aux的多态对象,将cplx的延迟绑定的实现转发到该对象
或者/更一般地说,与其直接在aux
组件上操作fun1
,不如通过绑定在该组件的等效组件上操作。例如:
module aux_module
implicit none
type :: aux0
integer :: a
end type aux0
type, extends(aux0) :: aux1
integer :: b
end type aux1
type, extends(aux0) :: aux2
integer :: c
end type aux2
contains
! Really the logic in `fun1` from the question's example code
! doesn't have to be within a binding. It could be factored out.
subroutine proc2(aux)
class(aux0), intent(in) :: aux(:)
integer :: i
do i = 1, size(aux)
print *, aux(i) % a
end do
end subroutine proc2
end module aux_module
module cplx_module
use aux_module
implicit none
type, abstract :: cplx0
contains
! Does this have to be a binding?
procedure :: proc1
procedure(cplx0_get_aux), deferred :: get_aux
end type cplx0
interface
function cplx0_get_aux(c)
import cplx0
import aux0
implicit none
class(cplx0), intent(in), target :: c
! we return a pointer in case we want it to be on the
! left hand side of an assignment statement.
class(aux0), pointer :: cplx0_get_aux(:)
end function cplx0_get_aux
end interface
type, extends(cplx0) :: cplx1
type(aux1) :: aux(2)
contains
procedure :: get_aux => cplx1_get_aux
end type cplx1
type, extends(cplx0) :: cplx2
type(aux2) :: this_doesnt_have_to_be_called_aux(3)
contains
procedure :: get_aux => cplx2_get_aux
end type cplx2
contains
! The internals of this could just forward to proc2.
subroutine proc1(self)
class(cplx0), target :: self
integer :: i
associate(the_aux => self%get_aux())
do i = 1, size(the_aux)
print *, the_aux(i) % a
end do
end associate
end subroutine proc1
function cplx1_get_aux(c)
class(cplx1), intent(in), target :: c
class(aux0), pointer :: cplx1_get_aux(:)
cplx1_get_aux => c%aux
end function cplx1_get_aux
function cplx2_get_aux(c)
class(cplx2), intent(in), target :: c
class(aux0), pointer :: cplx2_get_aux(:)
cplx2_get_aux => c%this_doesnt_have_to_be_called_aux
end function cplx2_get_aux
end module cplx_module
program p
use cplx_module
implicit none
type(cplx1) :: c1
type(cplx2) :: c2
c1 = cplx1([aux1(a=1,b=2), aux1(a=11,b=22)])
call c1%proc1
! call proc2(c1%aux)
c2 = cplx2([aux2(a=1,c=2), aux2(a=11,c=22), aux2(a=111,c=222)])
call c2%proc1
! call proc2(c2%this_doesnt_have_to_be_called_aux)
end program p