The answer is yes and no.
To the exact question posed: no, arrays are homogenous. This is required so
that array subscripting works properly.
However, you can achieve the same effect by encapsulating the array inside
another type, e.g.
TYPE poly_type
CLASS(base_typ),ALLOCATABLE :: val
END TYPE
TYPE(poly_type),ALLOCATABLE :: ab_obj(:)
ALLOCATE(ab_obj(2))
ALLOCATE(a_typ::ab_obj(1)%val)
ALLOCATE(b_typ::ab_obj(2)%val)
!
! Now ab_obj(1)%val is a_typ, but ab_obj(2)%val is b_typ.
Hope that helps,
Cheers,
-----Original Message-----
From: Stefan Ano
Date: 平成 25年7月23日 17:35
To: [log in to unmask]
Subject: [COMP-FORTRAN-90] Array of mixed dynamic type
Hi there!
I wonder weather Fortran arrays can hold objects of mixed dynamic
type? Well, let me point this out by an example:
> PROGRAM MAIN
>
> TYPE, ABSTRACT :: base_typ
> END TYPE
> TYPE, EXTENDS(base_typ) :: a_typ
> INTEGER :: a
> END TYPE
> TYPE, EXTENDS(base_typ) :: b_typ
> REAL :: b
> END TYPE
> ! May hold a_typ or b_typ
> CLASS(base_typ), ALLOCATABLE :: ab_obj(:)
> ALLOCATE( ab_obj(2), source=[a_typ(1),a_typ(2)] )
> ! This makes ab_obj to be of size 2 and dynamic type a_typ
>
> DEALLOCATE( ab_obj )
> ALLOCATE( ab_obj(2), source=[b_typ(1.0),b_typ(2.0)] )
> ! Well, it could also hold b_typ
>
> DEALLOCATE( ab_obj )
> ! However, holding mixed types does not work
> ALLOCATE( ab_obj(2), source=[a_typ(1),b_typ(2.0)] )
> ! and this is exactly what I want
> ! Is there a way around? END PROGRAM
What I would like to have is that the first array element ab_obj(1) is
of dynamic type a_typ whereas ab_obj(2) is b_typ. Is there a way to
achieve this?
Thank you very much for your help.
Cheers,
Stefan
--
................................Malcolm Cohen, Nihon NAG, Tokyo.
|