Showing results for 
Search instead for 
Did you mean: 

allocatable class(*) function return

So i have this piece of code:

module asd
        function return_alloc() result(res)
            class(*), dimension(:), allocatable :: res
            integer, dimension(3) :: x
            x = 5
            allocate(res, source = x)
            print *, transfer(res,(/1,1,1/))
        subroutine print_x()
            class(*), dimension(:), allocatable :: test
            allocate(test, source = return_alloc())
            print *, transfer(return_alloc(),(/1,1,1/)), "aa"
            print *, transfer(test,(/1,1,1/)), "bb", size(test)
program xyxcxvxvcxcyc
    use asd
    call print_x()

The output is:

          5           5           5
          5           5           5
bb           3

while it shoud be:

          5           5           5
          5           5           5
          5           5           5 aa
          5           5           5 bb           3

So for some reason the function return is empty but retained it's dimensions.

my question now is obviously what am I doing wrong?

0 Kudos
4 Replies
Valued Contributor III

IIRC, there is an open

IIRC, there is an open incident with Intel Fortran with the use of TRANSFER intrinsic in IO (e.g., PRINT statements) but presently I'm unable to locate any threads.  Intel Fortran team might be to find the details, if this is correct.

0 Kudos
Black Belt

I have to ask - what is it

I have to ask - what is it you are really trying to do here? You seem to be abusing TRANSFER in my opinion.

At first I thought that it was not allowed for the SOURCE argument to TRANSFER be polymorphic, much less unlimited poiymorphic, but interp F03/0047 covered this and it is allowed. I think this should have worked as you desired.

Steve (aka "Doctor Fortran") -
0 Kudos

I was unable to find other

I was unable to find other reports with TRANSFER in I/O but I’ll keep looking. For now, I did reproduce/report the findings here to Development.

(Internal tracking id: DPD200419455)

0 Kudos

Then I also have found

Then I also have found another interesting behaviour with this piece of code.

If I change line 14 to

print *, return_alloc(), "aa"

it compiles and gives the same output.

But if I change line 15 in the same manner, I get an error message saying i have to provide a user-defined I/O routine for polymorphic items, which should also apply to the above case.

0 Kudos