【发布时间】:2019-05-08 14:06:00
【问题描述】:
我正在用新功能更新一些代码,理想情况下,我的解决方案将包括一个数组,其中每个元素都是不同类型的,这在 fortran 中是不可能的。所以我尝试使用多态对象数组,但在初始化数组元素后我似乎无法调用任何类型绑定子例程。
这是问题的要点。
类型声明模块:
type :: fruit
end type fruit
type, extends(fruit) :: apples
contains
procedure :: init => init_apples
end type apples
type, extends(fruit) :: oranges
contains
procedure :: init => init_oranges
end type oranges
contains
pure subroutine init_apples(me)
class(apples), intent(inout) :: me
! Do Stuff
end subroutine init_appples
pure subroutine init_oranges(me)
class(oranges), intent(inout) :: me
! Do Stuff
end subroutine init_oranges
然后在主程序中:
use apropriate module
type fruit_basket
class(fruit), allocatable :: item
end type
type(fruit_basket), allocatable :: gift(:)
allocate(gift(2))
!Option 1
allocate(apples::gift(1).item)
allocate(oranges::gift(2).item)
!Option 2
gift(1) = fruit_basket(apples)
gift(2) = fruit_basket(oranges)
编译器接受上述任一选项。现在理想情况下,我想为其中一个元素调用 init
!Tried this
call gift(1).init()
!Also tried this
call gift(1).item.init()
其中任何一个都会产生错误: 错误 #6460:这不是包含结构中定义的字段名称。 [初始化]
我做错了什么?
编辑:
所以我有点让它工作,对它的工作方式不是很满意,但我想它会做的:
类型声明:
Module type_declaration
implicit none
type, abstract, public :: Base_Type
contains
procedure(init_base), deferred :: init
procedure(get_base), deferred :: GetResult
end type
interface
pure subroutine init_base(this, x)
import Base_Type
class(base_type), intent(inout) :: this
real(8), intent(in) :: x
end subroutine
real(8) pure function get_base(this)
import Base_Type
class(base_type), intent(in) :: this
end function
end interface
type, extends(Base_Type) :: Subtype1
real(8) alpha
contains
procedure :: init => init_type1
procedure :: GetResult => get_res_type1
end type
type, extends(Base_Type) :: Subtype2
real(8) alpha, beta
contains
procedure :: init => init_type2
procedure :: GetResult => get_res_type2
end type
contains
pure subroutine init_type1(this, x)
class(Subtype1), intent(inout) :: this
real(8), intent(in) :: x
!Work here
this.alpha = x * 2.
end subroutine
pure subroutine init_type2(this, x)
class(Subtype2), intent(inout) :: this
real(8), intent(in) :: x
!Work here
this.alpha = x * 2
this.beta = x / 3.
end subroutine
real(8) pure function get_res_type1(this)
class(Subtype1), intent(in) :: this
get_res_type1 = this.alpha
end function
real(8) pure function get_res_type2(this)
class(Subtype2), intent(in) :: this
get_res_type2 = this.alpha + this.beta
end function
end module type_declaration
主程序变体 1:
program Polymorhic_Test
use type_declaration
implicit none
type data_container
class(Base_type), allocatable :: item
end type
type(data_container) :: MainArray(2)
real(8) :: x, y = 0.
character(10) :: cDummy
allocate(Subtype1::MainArray(1).item)
allocate(Subtype2::MainArray(2).item)
x = 1.
call MainArray(1).item.init(x)
y = MainArray(1).item.GetResult()
x = 2.
call MainArray(2).item.init(x)
y = MainArray(2).item.GetResult()
read cDummy
end program Polymorhic_Test
主程序变体 2
program Polymorhic_Test
use type_declaration
implicit none
type data_container
class(Base_type), allocatable :: item
end type
type(data_container) :: MainArray(2)
real(8) :: x, y = 0.
character(10) :: cDummy
type(Subtype1) :: ST1
type(Subtype2) :: ST2
x = 1.
call ST1.init(x)
allocate(MainArray(1).item, source=ST1)
y = MainArray(1).item.GetResult()
x = 2.
call ST2.init(x)
allocate(MainArray(2).item, source=ST2)
y = MainArray(2).item.GetResult()
read cDummy
end program Polymorhic_Test
我可能会选择选项 2,因为这样我就不必在基本类型中延迟 init,因为我的子类型将具有不同数量的 init 所需的参数,而且我不喜欢使用基本类型的想法输入带有 20 个可选参数的 init。
【问题讨论】:
-
gift(1)是声明类型fruit_basket和gift(1)%item是声明类型fruit,两者都没有类型绑定过程init。如果您不了解这些方面,那么我们可能会找到相关问题。 -
考虑例如this question。
-
@francescalus 感谢您提供问题的链接。不幸的是,这正是我试图避免的。这个想法是使用自己的一组(可能不重叠)子例程和参数来扩展类型。但是如果我不得不推迟基本类型中的整个混乱,它就会使整体不那么有吸引力。我的意思是它是有道理的,编译器应该如何知道我所指的扩展类型。我的印象是有办法解决这个问题。
标签: fortran