I want to understand if the following Fortran code complies with the Fortran 2008 standard.
module m
implicit none
type t
integer, allocatable, dimension(:) :: r
end type t
contains
function tt(a,b)
implicit none
type(t), allocatable, dimension(:) :: tt
type(t), intent(in), dimension(:) :: a,b
allocate(tt, source = [a,b]) ! Is this square bracket correct ?
return
end function tt
function ts(arg)
implicit none
type(t), allocatable, dimension(:) :: ts
integer, intent(in) :: arg(:)
allocate(ts(1))
allocate(ts(1)%r, source = arg)
return
end function ts
end module m
program p
use m
implicit none
type(t), dimension(2) :: c
c=tt(ts([99,199,1999]),ts([42,142]))
if (any (c(1)%r .ne. [99,199,1999])) STOP 1
if (any (c(2)%r .ne. [42,142])) STOP 2
end program p
This above code works fine with gfortran-9.4.0 and generates the expected output.
Since I am a beginner in Fortran, I got confused by a square bracket expression (marked in the code).
Inside the function tt, both a and b are t type arrays. In this example, both of them only have array size of 1.
What is [a,b]?
I think it is an array initialized with square brackets, but each of its element is a t type array. (Not a t type scalar)
What is [a,b]'s rank? Does it match the return value tt which is also a t type allocatable array?
From Fortran 2008 standard
Each allocate-object shall be type compatible (4.3.1.3) with source-expr . If SOURCE= appears, source-expr shall be a scalar or have the same rank as each allocate-object.