실제 데이터 유형과 복잡한 데이터 유형의 차이점을 숨기려고합니다. FORTRAN 2003에서는 그렇게 할 수있는 방법이있을 것이라고 생각합니다.FORTRAN : data polymorphic
목표는 런타임에 유형을 결정할 수있는 다형성 할당 가능 배열을 정의하는 것입니다. 게다가 다형성 배열을 사용하여 대수를 수행하는 서브 루틴이 있습니다 (실제 및 복합 데이터에 대해 동일한 방정식이 작동합니다). 그럼 난 데 문제
module poly
implicit none
private
type, public :: MyType
class(*), allocatable :: AllData(:)
contains
procedure, public :: Constructor
endtype MyType
contains
subroutine Constructor(this, Nsize, IfComplex)
class(MyType), intent(inout) :: this
integer, intent(in) :: Nsize
logical, intent(in) :: IfComplex
if(IfComplex) then
allocate(complex(8)::this%AllData(Nsize))
else
allocate(real(8)::this%AllData(Nsize))
endif
endsubroutine
endmodule poly
! Same algebra subroutine
! Main
program test
use poly
use Operation
type(MyType) :: t1, t2
integer :: i
call t1%Constructor(4, .true.)
call Square(t1%AllData)
endprogram test
:
접근 A : :
module poly
implicit none
private
type, abstract, public :: MyType
contains
procedure, public :: Constructor
endtype MyType
type, extends(MyType), public :: MyTypeR
real(8), allocatable :: AllData(:)
endtype MyTypeR
type, extends(MyType), public :: MyTypeI
complex(8), allocatable :: AllData(:)
endtype MyTypeI
contains
subroutine Constructor(this, Nsize)
class(MyType), intent(inout) :: this
integer, intent(in) :: Nsize
select type(this)
type is(MyTypeR)
allocate(this%AllData(Nsize))
type is(MyTypeI)
allocate(this%AllData(Nsize))
endselect
endsubroutine
endmodule poly
! Algebra subroutine
module Operation
contains
subroutine Square(Array)
class(*), intent(inout) :: Array(:)
select type(Array)
class is(real(8))
Array = Array**2
class is(complex(8))
Array = Array**2
endselect
endsubroutine Square
endmodule Operation
! Main
program test
use poly
use Operation
class(MyType), allocatable :: t1, t2
integer :: i
logical :: IfComplex = .true.
if(IfComplex) then
allocate(MyTypeI::t1)
else
allocate(MyTypeR::t1)
endif
call t1%Constructor(4)
call Square(t1%AllData)
endprogram test
접근 B (무제한 다형성 할당 가능한 변수)
은 그것을하기 위해서, 나는 두 가지 시도를 대수학 서브 루틴을 두 접근법과 함께 사용합니다. 본질적인 대입 문에서 변수는 다형성이 아니어야합니다. 모든 제안을 주시면 감사하겠습니다.
위대한 답변. Intrinsic 함수 지원 (할당 포함)이 허용되면 큰 기능입니다. – FortCpp
두 가지 접근 방식에 대한 제안에 감사드립니다. 컴파일 문제가 해결되었습니다. 제가 잠시 동안 실제 (8)과 복잡한 (8)만을 다루고 있다고 가정합시다 (두 번째 접근법에서 몇 가지 세부 사항을 생략했습니다). 어떤 접근 방식이 자신의 의견에 더 낫습니까? – FortCpp