Initialize parametric-size array of parameterized derived type in Fortran?

Viewed 358

Fortran allows parametrizing the size of elements of derived types. However, where fixed-size elements can have a default value assigned in the type declaration, there doesn't seem to be a way for parametrized entries:

PROGRAM main
  IMPLICIT NONE

  TYPE data1
     INTEGER :: array(5) = 2   ! allowed
  END type data1

  TYPE data2(n)
     INTEGER, LEN :: n
     INTEGER :: array(n) = 2   ! incorrect: error #8737 with intel fortran 19,
  END type data2               !            ignored by gfortran 8.2.1

END PROGRAM main

Assigning default values is convenient, as it allows avoiding repeating the initialization every time the type is used, but for parametric-sized fields it isn't allowed; Gfortran just ignores the default value silently, and Intel Fortran issues an error

error #8737: For a default initialized component every type parameter and array bound
             must be a constant expression.   [ARRAY]

Is there any syntax, that would allow defining a default value after all?

3 Answers

There can be no default initialization for such components.

As the Intel Fortran error message states, the array bounds for a component with an initialization expression must be constant expressions (this is constraint C762 of Fortran 2018). The length type parameter is not usable as a constant expression.

There is no other syntax to specify a default value for the component.

A kind type parameter can feature in a constant expression, so components with bounds given by a kind parameter of that type can have default initialization.

You can create a constructor that takes the length parameter to create the object

module datatypes

type data2(n)
    integer, len :: n
    integer :: array(n)
contains        
    procedure, pass :: data2_fill2
end type

interface data2
    module procedure new_data2
end interface

contains
    subroutine data2_fill2(this)
        class(data2(*)) :: this
        this%array = 2
    end subroutine
    function new_data2(n) result(r)
        integer, intent(in) :: n
        type(data2(n)) :: r
        call r%data2_fill2()
    end function
end module

program Main
use datatypes
    type(data2(3)) :: mydata

    mydata = data2(100)

    print *, "Size of array ", size(mydata%array)

    if( mydata%array(1) /= 2) then
        print *, "Something went wrong"
    end if


end program

You have found a bug in different compilers. Your code is standard conforming. Fleshing out the code a bit, the following should print '2 2 2'.

program main

implicit none
!
! F2018, 7.5.1, page 64: A derived type can be parameterized by one or
! more type parameters, each of which is defined to be either a kind
! or length type parameter and can have a default value.
!
! F2018, 7.5.3.1, page 69: A type parameter may be used as a primary in
! a specification expression (10.1.11) in the derived-type-def.
!
! 10.1.11 Specification expression (page 156)
! ...
!    R1028 specification-expr  is scalar-int-expr
!
! C1010 (R1028) The scalar-int-expr shall be a restricted expression.
!
! A restricted expression is an expression in which each operation is
! intrinsic or defined by a specification function and each primary is
! ...
! (13) a type parameter of the derived type being defined,
!
type data2(n)
   integer, len :: n
   integer :: array(n) = 2
end type data2

type(data2(n=3)) :: a

print *, a%array  ! This should print 2 2 2

end program main

gfortran compiles the code, but prints '0 0 0', so gfortran has a bug in applying the component initialization.

Related