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

Initialize a default value for a derived type component variable.

OP1
New Contributor III
1,514 Views
I am not sure about the syntax for assigning a default value to a component of a derived type variable. I want all new instances of my derived type have some of their components assigned a default value.
For instance:

TYPE MY_TYPE
LOGICAL IS_IT_INITIALIZED
INTEGERT
...
END TYPE MY_TYPE

Now, when I declare a variable A of type MY_TYPE, I want to make sure that the component IS_IT_INITIALIZED is set to .FALSE. and T to 1000 (for instance).
Is there a way to do this? Or should I manually initialize every new instance of MY_TYPE when I declare it?

Thanks!

Olivier
0 Kudos
3 Replies
Les_Neilson
Valued Contributor II
1,514 Views
Quoting - opmkl
I am not sure about the syntax for assigning a default value to a component of a derived type variable. I want all new instances of my derived type have some of their components assigned a default value.
For instance:

TYPE MY_TYPE
LOGICAL IS_IT_INITIALIZED
INTEGERT
...
END TYPE MY_TYPE

Now, when I declare a variable A of type MY_TYPE, I want to make sure that the component IS_IT_INITIALIZED is set to .FALSE. and T to 1000 (for instance).
Is there a way to do this? Or should I manually initialize every new instance of MY_TYPE when I declare it?

Thanks!

Olivier

Yes you can do it.
TYPE MY_TYPE
LOGICAL IS_IT_INITIALIZED = .FALSE.
INTEGER T = 1000
...
END TYPE MY_TYPE

Then TYPE(MY_TYPE) AnOther will take the default values

Les
0 Kudos
Steven_L_Intel1
Employee
1,514 Views
Almost. You need to use:

LOGICAL :: IS_IT_INITIALIZED = .FALSE.
INTEGER :: T = 1000

If you're going to supply an initial value, the :: delimiter is required.
0 Kudos
OP1
New Contributor III
1,514 Views

Aah... that's the thing I was also missing... The "::" delimiter...

Thanks a lot Steve. This is a very convenient feature.

Olivier
0 Kudos
Reply