Skip to content

Extend move_alloc to unlimited polymorphic variables #345

Open
@ivan-pi

Description

@ivan-pi

Originally discussed here: https://fortran-lang.discourse.group/t/ownership-for-fortran-pointers/8084/55?u=ivanpribec

The proposal would be to provide a means of move allocation from unlimited polymorphic to other allocatable variables (assuming that the types are consistent).

type :: heavy_type
   real :: a(1000)
end type

class(*), allocatable :: a1
class(heavy_type), allocatable :: a2

allocate(a2)
call move_alloc(from=a2,to=a1)  ! Compiles with gfortran, ifx, and flang

! Fails (from and to must be of the same type and kind)
call move_alloc(from=a1,to=a2) 

select type(a1)
class is (heavy_type)
    ! Fails (from argument must be allocatable)
    call move_alloc(from=a1,to=a2) 
end select

end

The standard currently says in 11.1.3.3

The associating entity does not have the ALLOCATABLE or POINTER attributes;

Just a thought, perhaps the semantics of move_alloc could be extended, such that:

call move_alloc(from,to[,stat])

would apply to any two allocatable variables including polymorphic ones. When types don't belong to the same hierarchy, it should obviously fail. In the case that from and to are both polymorphic it requires some thought of what should happen allocating from child to parent (some data members would be destroyed) or parent to child (some data members would remain uninitialized).

cc @everythingfunctional @gronki

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions