- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi All,
I am trying to create a derived data type that can at run time take on different values. The follow is a snipit of code:
[fortran]
PROGRAM TEST
!
TYPE FLEXIBLE
CLASS(*), ALLOCATABLE:: VAL
END TYPE
TYPE(FLEXIBLE)::A
!
CLASS(*), ALLOCATABLE:: B
INTEGER:: I
!IT SEEMS LIKE TH FOLLOWING SHOULD WORK:
I=5
ALLOCATE(A%VAL,SOURCE=I)
ALLOCATE(B,SOURCE=I)
!
A%VAL=10
B=50
!
END PROGRAM
[/fortran]
When I try to set the value to a new value, I get a compiler error. Even if I remove the A%VAL= and B= they do not seem to hold the value of I.
On a side note, what does MOLD do within an ALLOCATE statement.
Thanks as always for your help.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You need a SELECT TYPE construct - for an unlimited polymorphic item, you can access it as a typed variable only inside a SELECT TYPE (with appropriate TYPE IS block.)
PROGRAM TEST ! TYPE FLEXIBLE CLASS(*), ALLOCATABLE:: VAL END TYPE TYPE(FLEXIBLE)::A ! CLASS(*), ALLOCATABLE:: B INTEGER:: I !IT SEEMS LIKE TH FOLLOWING SHOULD WORK: I=5 ALLOCATE(A%VAL,SOURCE=I) ALLOCATE(B,SOURCE=I) ! SELECT TYPE (AVAL=>A%VAL) TYPE IS (INTEGER) PRINT *, AVAL AVAL = 10 PRINT *, AVAL END SELECT SELECT TYPE (B) TYPE IS (INTEGER) PRINT *, B B = 50 PRINT *, B END SELECT ! END PROGRAM
MOLD= specifies the type, type parameters and shape but not the value. SOURCE= does all that plus copies the value.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you so much, it is a shame you can not directly access it, but it clears up a lot of the problems I have been having.
My only question is what would be the keyword for DOUBLE PRECISION or be distinguish between different REAL precision and how would they be applied with a MOLD= statement.
[fortran]
PROGRAM TEST
!
CLASS(*), ALLOCATABLE:: B
ALLOCATE(B,MOLD=REAL)
!
SELECT TYPE (B)
TYPE IS(REAL)
!
B=50.
WRITE(*,*) B
!
END SELECT
Thanks
END PROGRAM
[/fortran]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You would use:
TYPE IS (REAL(8))
Your use of MOLD= is not correct - you put another variable there. You can do this instead:
ALLOCATE (REAL(8)::B)

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page