Array constructors

An array constructor is a sequence of specified scalar values. It constructs a rank-one array whose element values are those specified in the sequence. You can construct arrays of rank greater than one using an intrinsic function. See RESHAPE(SOURCE, SHAPE, PAD, ORDER) for details.

Syntax

Read syntax diagramSkip visual syntax diagram
>>-+-(/ac_spec/)-+---------------------------------------------><
   '-[ ac_spec ]-'   

where ac_spec is:
Read syntax diagramSkip visual syntax diagram
>>-+-i_d_type_spec--::-------------------+---------------------><
   |                        .-,--------. |   
   |                        V          | |   
   '-+-------------------+----ac_value-+-'   
     '-i_d_type_spec--::-'                   

i_d_type_spec

Is an intrinsic_type_spec or derived_type_spec. For a list of possible type specifications, see Type Declaration.

You cannot use BYTE as an intrinsic_type_spec in an array constructor.

ac_value
is an expression or implied-DO list that provides values for array elements.

Rules

An i_d_type_spec specifies the type and type parameters of the array constructor. Each ac_value expression must be compatible with intrinsic assignment to a variable with the type and type parameters. Each value is converted to the type parameters of the array constructor.
  • If you specify an intrinsic type, each ac_value expression in the array constructor must be of an intrinsic type compatible with the type you specify.
  • If you specify a derived type, all ac_value expressions in the array constructor must be of that derived type and have the same kind type parameter values as the type you specify.

If i_d_type_spec is omitted, each ac_value expression in the array constructor must have the same type and type parameters.

If i_d_type_spec appears without an ac_value, a zero-sized rank-one array is created.

The ac_value complies with the following rules:
  • If it is a scalar expression, its value specifies an element of the array constructor.
  • If it is an array expression, the values of the elements of the expression, in array element order, specify the corresponding sequence of elements of the array constructor.
  • If it is an implied-DO list, it is expanded to form an ac_value sequence under the control of the implied_do_variable, as in the DO construct.
Fortran 2003 begins

If ac_value is a polymorphic entity, its declared type is used. Because unlimited polymorphic entities have no declared type, you cannot use them for ac_value.IBM extension beginsIf you compile your program with -qxlf2003=dynamicacval, the dynamic type of ac_value is used, and you can use unlimited polymorphic entities for ac_value. IBM extension ends For more details about using polymorphic entities for ac_value, see Example 2.

For more information about unlimited polymorphic entities, the declared and dynamic types of these entities, see Polymorphic entities.

Fortran 2003 ends

Examples

Example 1: Different array constructors
INTEGER, DIMENSION(5) :: a, b, c, d(2, 2)
CHARACTER(5), DIMENSION(3) :: color

! Assign values to all elements in a
a = (/1, 2, 3, 4, 5/)               

! Assign values to some elements
a(3:5) = (/0, 1, 0/)

! Construct temporary logical mask
c = MERGE(a, b, (/T, F, T, T, F/))
  
! The array constructor produces a rank-one array, which
! is turned into a 2x2 array that can be assigned to d.
d = RESHAPE(SOURCE = (/1, 2, 1, 2/), SHAPE = (/2, 2/))

! Here, the constructor linearizes the elements of d in
! array-element order into a one-dimensional result.
PRINT *, a((/d/))

! Without a type_spec,each character literal must be of length 5
color = ['RED  ', 'GREEN', 'BLUE ']

! With a type_spec, padding and truncation of each character literal occurs
color = [CHARACTER(5) :: 'RED', 'GREEN', 'BLUE']
Fortran 2003 begins
Example 2: Polymorphic entities as ac_value
PROGRAM PolyAcValues
  TYPE base
    INTEGER :: i
  END TYPE

  TYPE, EXTENDS(base) :: child
    INTEGER :: j
  END TYPE

  TYPE(base) :: baseType = base(3)
  TYPE(child) :: childType = child(4, 6)
  
  ! Declare a polymorphic entity of base type
  CLASS(base), ALLOCATABLE :: baseClass
  ! Declare an unlimited polymorphic entity. It has no declared type. 
  ! Its dynamic type can be any derived type or intrinsic type
  CLASS(*), ALLOCATABLE :: anyClass
  ! Declare a deferred-shape array of unlimited polymorphic entities
  CLASS(*), ALLOCATABLE :: anyClassArr(:)

  ! Allocate a child item to baseClass. The dynamic type of bassClass is child.
  ALLOCATE(baseClass, source = childType)
  ! Polymorphic entities used in the array constructor
  ALLOCATE(anyClassArr(2), source = [baseClass, baseClass])
  
  ! Because the compiler uses the declared type, which is base, and the result 
  ! is "Base item: 4 4". If you specify -qxlf2003=dynamicacval, the compiler uses 
  ! the dynamic type, which is child, and the result is "Child item: 4,6 4,6".
  CALL printAny(anyClassArr, 2)
  DEALLOCATE(anyClassArr)
  DEALLOCATE(baseClass)

  ! Allocate a base item to anyClass. The dynamic type of anyClass is base.
  ALLOCATE(anyClass, source = baseType)
  ! Unlimited polymorphic entities used in the array constructor
  ALLOCATE(anyClassArr(2), source = [anyClass, anyClass])
  
  ! If you specify -qxlf2003=dynamicacval, the use of unlimited polymorphic
  ! entities in the array constructor is valid, and the compiler uses the 
  ! dynamic type, which is base. The result is "Base item: 3 3"; Otherwise, 
  ! a severe error is issued at compile time.
  CALL printAny(anyClassArr, 2)
  DEALLOCATE(anyClassArr)
  DEALLOCATE(anyClass)

  CONTAINS
    SUBROUTINE printAny(printItem, len)
      CLASS(*) :: printItem(len)

      DO i = 1, len
        SELECT TYPE (item => printItem(i))
          TYPE IS (base)
            PRINT *, 'Base item: ', item
          TYPE IS (child)
            PRINT *, 'Child item: ', item
        END SELECT
      END DO
    END SUBROUTINE
END PROGRAM
Fortran 2003 ends


Voice your opinion on getting help information Ask IBM compiler experts a technical question in the IBM XL compilers forum Reach out to us