Structure constructors 
Author Message
 Structure constructors

[ question.txt 1K ]
Question re: structure constructors

The DVF Lang.Ref.Manual, section 3.2.4, p3-27, states that "structure
constructors let you specify _scalar_ values of a derived type".
How does one initialize _arrays_ of a derived (multicomponent) type?

Suppose I want to initialize the members of a structure array:

  TYPE mfr_mdl
    CHARACTER(LEN=20)::name
    INTEGER::id
  END TYPE mfr_mdl

  TYPE(mfr_mdl),DIMENSION(10)::du_mod    ! an array of this type

  du_mod( 1) ('IV25M',mdl_du_iv25m)      ! is this ok?
  du_mod( 2) ('IV50M',mdl_du_iv50m)      !  the mdl_du_etc are all
  du_mod( 3) ('IV1  ',mdl_du_iv1)        !  defined integer constants
  du_mod( 4) ('IV5  ',mdl_du_iv5)
  du_mod( 5) ('IV10 ',mdl_du_iv10)
  du_mod( 6) ('II420',mdl_du_ii420)
  du_mod( 7) ('IAC5 ',mdl_du_iac5)
  du_mod( 8) ('OAC5 ',mdl_du_oac5)
  du_mod( 9) ('ITCJ ',mdl_du_itcj)
  du_mod(10) ('IIAC5',mdl_du_iiac5)

When is the structure constructor implemented?  At startup only?  Is this
the rough equivalent of a DATA statement?  Where should structure
constructors be placed (in a MODULE or within actual code, before or
mixed with executable statements, etc.)?  The references I have
consulted are silent on these details.




Wed, 18 Jun 1902 08:00:00 GMT  
 Structure constructors

Quote:

> Question re: structure constructors

> The DVF Lang.Ref.Manual, section 3.2.4, p3-27, states that "structure
> constructors let you specify _scalar_ values of a derived type".
> How does one initialize _arrays_ of a derived (multicomponent) type?

You can initialize an array of a derived type two ways.  It's just
the same as ordinary intrinsic type arrays; only there's more
typing because of all of the ocmponents.  If you want to initialize
them all to the same value you can broadcast a scalar.

   TYPE(mfr_mdl),DIMENSION(10)::du_mod = mfr_mdl('fred',3)  
sets all 10 elements to the structure value  'fred',3

From your example it doesn't look like this is what you want, so you
need an array constructor

   TYPE(mfr_mdl),DIMENSION(10)::du_mod  = (/   &
   mfr_mdl ('IV25M',mdl_du_iv25m)      ,&
   mfr_mdl ('IV50M',mdl_du_iv50m)      ,&
   mfr_mdl ('IV1  ',mdl_du_iv1)       , &
   mfr_mdl ('IV5  ',mdl_du_iv5)      , &
   mfr_mdl ('IV10 ',mdl_du_iv10)      ,&
   mfr_mdl ('II420',mdl_du_ii420)     ,&
   mfr_mdl ('IAC5 ',mdl_du_iac5)      ,&
   mfr_mdl ('OAC5 ',mdl_du_oac5)      ,&
   mfr_mdl ('ITCJ ',mdl_du_itcj)      ,&
   mfr_mdl ('IIAC5',mdl_du_iiac5)     ,&
   /)

If you had a higher dimensioned array you can use the RESHAPE function.
Something like:

 TYPE(mfr_mdl),DIMENSION(5,2)::du_mod  = reshape ( (/   &
   mfr_mdl ('IV25M',mdl_du_iv25m)      ,&
   mfr_mdl ('IV50M',mdl_du_iv50m)      ,&
...
     /),   (/5,2/) )

I think there is a way to avoid using all of the "mfr_mdl" structure
constructors in the array constructor and just string out the different
constants.  But I think that is a bad way to do things, so I won't even
mention it, not will I mention the SEQUENCE keyword!

Quote:

> Suppose I want to initialize the members of a structure array:

>   TYPE mfr_mdl
>     CHARACTER(LEN=20)::name
>     INTEGER::id
>   END TYPE mfr_mdl

>   TYPE(mfr_mdl),DIMENSION(10)::du_mod    ! an array of this type

>   du_mod( 1) ('IV25M',mdl_du_iv25m)      ! is this ok?
>   du_mod( 2) ('IV50M',mdl_du_iv50m)      !  the mdl_du_etc are all
>   du_mod( 3) ('IV1  ',mdl_du_iv1)        !  defined integer constants
>   du_mod( 4) ('IV5  ',mdl_du_iv5)
>   du_mod( 5) ('IV10 ',mdl_du_iv10)
>   du_mod( 6) ('II420',mdl_du_ii420)
>   du_mod( 7) ('IAC5 ',mdl_du_iac5)
>   du_mod( 8) ('OAC5 ',mdl_du_oac5)
>   du_mod( 9) ('ITCJ ',mdl_du_itcj)
>   du_mod(10) ('IIAC5',mdl_du_iiac5)

> When is the structure constructor implemented?  At startup only?  Is this
> the rough equivalent of a DATA statement?  Where should structure
> constructors be placed (in a MODULE or within actual code, before or
> mixed with executable statements, etc.)?  The references I have
> consulted are silent on these details.

A structure constructor can appear in the declaratives to initialize
things, that's what your example does.  The standard doesn't require
the processor to figure out the values at compile time; but I think
all compilers do it that way.  So in that sense it's like what most
people think of as a DATA statement.  When used this way all of the
thingo's in the constructor have to be "initialization expressions"
which are essentially constants or simple functions of constants
(things like TRIM('abc   ').  The variable du_mod is giving the
initial values.

This kind of initialization also implies the SAVE attribute, so if
you change du_mod someplace the changes will persist.  du_mod does
NOT get reinitialized everytime you exit and reenter a subroutine.
If you know for sure that du_mod will never be changed you can
make it a PARAMETER via
 TYPE(mfr_mdl),DIMENSION(10), PARAMETER::du_mod  = (/   &
   mfr_mdl ('IV25M',mdl_du_iv25m)      ,&
...

This is the same as the familiar  REAL, PARAMETER  ::  PI = 3.14;
just a few more card punches.

You can also use the constructors as run time expressions, something
like:

    du_mod  = (/   &
   mfr_mdl ('IV25M'//something_else, 10*mdl_du_iv25m)      ,&
   mfr_mdl ('IV50M',max(mdl_du_iv50m,23))      ,&
  ...
   /)

Hope this helps

Dick Hendrickson

- Show quoted text -

Quote:





Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Assigning values to a derived type (structure constructor?)

2. Evaluation order in structure constructors

3. Structures within Structures

4. Event case loses connection to data structure when data structure is modified

5. Event structure is not visible in the structures pallete

6. GOBO data structure library vs other data structure libraries

7. manipulating structures and arrays of structures

8. structure containing structure - newbie

9. Determining structure equivalence, not structure eq-ness.

10. How to test on circular structures,structures and instances of classes

11. Haskell Q: constructors vs. constructor classes?

12. Classes Constructors

 

 
Powered by phpBB® Forum Software