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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With