title: FORTRAN: data polymorphic
body: <p>I am trying to hide the difference between the real and complex data types. In FORTRAN 2003 I think there could be a way of doing so.</p>
  
  <p>The goal is to define a polymorphic allocatable array which type can be decided at the run-time. Plus there is a subroutine which takes the polymorphic array to do some algebra (same equation works for real and complex data).</p>
  
  <p>In order to do it, I made two attempts:</p>
  
  <p>Approach A:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Approach B (unlimited polymorphic allocatable variable):</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Then I am having problem with the algebra subroutine in both approaches: In an intrinsic assignment statement, variable shall not be polymorphic. Any suggestion will be appreciated.</p>