Intel® Fortran Compiler 16.0 User and Reference Guide

Structure Constructors

A structure constructor lets you specify scalar values of a derived type. It takes the following form:

d-name (comp-spec)

d-name

Is the name of the derived type. It cannot be an abstract type.

comp-spec

Is a component specification. Only one comp-spec can be specified for a component. However, you can specify more than one comp-spec in a constructor. A component specification takes the following form:

[keyword=] comp-data-source

keyword

Is the name of a component of the derived type d-name.

comp-data-source

Is one of the following:

  • expr
  • data-target
  • proc-target

expr

Is an expression specifying component values. The values must agree in number and order with the components of the derived type. If necessary, values are converted (according to the rules of assignment), to agree with their corresponding components in type and kind parameters.

data-target

Is a data pointer component. It must have the same rank as its corresponding component.

proc-target

Is a procedure pointer component.

Description

A structure constructor must not appear before its derived type is defined.

If keyword= appears, any expr is assigned to the component named by the keyword. If keyword= is omitted, each comp-data-source is assigned to the corresponding component in component order. The keyword= can be omitted from a comp-spec only if the keyword= has been omitted from each preceding comp-spec in the constructor, if any.

If a component of the derived type is an array, the shape in the expression list must conform to the shape of the component array.

If a component of the derived type is a pointer, the value in the expression list must evaluate to an object that would be a valid target in a pointer assignment statement. (A constant is not a valid target in a pointer assignment statement.)

If all the values in a structure constructor are constant expressions, the constructor is a derived-type constant expression.

The type name and all components of the type for which a comp-spec appears must be accessible in the scoping unit containing the structure constructor.

For a pointer component, the corresponding comp-data-source must be an allowable data-target or proc-target for such a pointer in a pointer assignment statement. If the comp-data-source is a pointer, the association of the component is that of the pointer; otherwise, the component is pointer-associated with the comp-data-source.

If a component with default initialization has no corresponding comp-data-source, then the default initialization is applied to that component.

If a component of a derived type is allocatable, the corresponding constructor expression must either be a reference to the intrinsic function NULL with no arguments or an allocatable entity of the same rank, or it must evaluate to an entity of the same rank.

If the expression is a reference to the intrinsic function NULL, the corresponding component of the constructor has a status of unallocated.

If the expression is an allocatable entity, the corresponding component of the constructor has the same allocation status as that allocatable entity. If the entity is allocated, the constructor component has the same dynamic type, bounds, and value. If a length parameter of the component is deferred, its value is the same as the corresponding parameter of the expression. Otherwise the corresponding component of the constructor has an allocation status of allocated and has the same bounds and value as the expression.

Examples

Consider the following derived-type definition:

  TYPE EMPLOYEE
    INTEGER ID
    CHARACTER(LEN=40) NAME
  END TYPE EMPLOYEE

This can be used to produce the following structure constructor:

  EMPLOYEE(3472, "John Doe")

The following example shows a type with a component of derived type:

  TYPE ITEM
    REAL COST
    CHARACTER(LEN=30) SUPPLIER
    CHARACTER(LEN=20) ITEM_NAME
  END TYPE ITEM

  TYPE PRODUCE
    REAL MARKUP
    TYPE(ITEM) FRUIT
  END TYPE PRODUCE

In this case, you must use an embedded structure constructor to specify the values of that component; for example:

  PRODUCE(.70, ITEM (.25, "Daniels", "apple"))

See Also