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
- 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
- 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.
- 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.

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.
If 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.
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.

Examples
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']

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




