Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Setting the value to CLASS(*)

ScottBoyce
Novice
536 Views

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.

0 Kudos
3 Replies
Steven_L_Intel1
Employee
536 Views

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.

0 Kudos
ScottBoyce
Novice
536 Views

 

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]

0 Kudos
Steven_L_Intel1
Employee
536 Views

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)

0 Kudos
Reply