Setting the value to CLASS(*)

Setting the value to CLASS(*)

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:

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

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.

4 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

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.

Steve - Intel Developer Support

 

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.

PROGRAM TEST 
!
CLASS(*), ALLOCATABLE:: B
ALLOCATE(B,MOLD=REAL)
!
SELECT TYPE (B)
TYPE IS(REAL)
!
B=50.
WRITE(*,*) B
!
END SELECT
 
Thanks
END PROGRAM

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)

Steve - Intel Developer Support

Leave a Comment

Please sign in to add a comment. Not a member? Join today