Issue with parameterised derived types

There are almost always workarounds for the broken PDT feature, so it is difficult for me to think of an example where PDT is required with no workaround. One example that comes to mind like this is the Euler Sum of Powers code that was posted in the Tutorial section here. Euler Sum of Powers Conjecture: Tutorial - #9 by RonShepard The euler2.F90 version (with the modulo arithmetic) was an ideal situation for PDTs. The integer values and the dimensions of the corresponding arrays were known only after some computations. The code could have been much simpler if it could have used PDTs. But there was a workaround that used allocatable arrays instead, so that was how it was written in order to make the code portable to the maximum number of compilers.

edit: Here is a small code that demonstrates some of the differences between PDTs with len parameters and allocatable components.

program xxx
   implicit none
   integer :: i, j, n
   type a_t
      integer, allocatable :: a(:)
   end type a_t
   type pdt_t(n)
      integer, len :: n
      integer :: a(n)
   end type pdt_t
   character(*), parameter :: cfmt='(*(g0:1x))'
   write(*,cfmt) 'input n:'
   read(*,*) n
   block
     type(pdt_t(n)) :: x(3)
     do concurrent (i=1:n)
        x(1:3)%a(i) = i
     enddo
     write(*,cfmt) x
   end block
   block
     type(a_t) :: x(3)
     do j = 1, size(x)
        allocate( x(j)%a(n) )
     enddo
     do concurrent (i=1:n)
        !x(1:3)%a(i) = i   ! this is not allowed for allocatable component.
     enddo
     do concurrent (i=1:n,j=1:size(x))
        x(j)%a(i) = i
     enddo
     !write(*,cfmt) x  ! this is not allowed for a derived type with allocatable components.
     write(*,cfmt) (x(j)%a(:), j=1,size(x))
   end block
end program xxx

As far as I know, this is standard fortran. This is simple enough to work with gfortran and nagfor, but it does not work with flang and lfortran. Notice how simple and straightforward the PDT block is compared to the allocatable block. In this case the workarounds aren’t too bad, just an extra couple of loops. And this is just for a single allocatable component – the workarounds can be much more extensive when there are several such components.

1 Like