Skip to content

Commit

Permalink
checking that allocate(array, source=other_array) is equivalent to ar…
Browse files Browse the repository at this point in the history
…ray = other_array
  • Loading branch information
pletzer committed Dec 5, 2024
1 parent f1819fd commit 3565c0b
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion class_vs_type/test_class_vs_type.f90
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ module myclass_mod
! methods
procedure :: init => myclass_init
procedure :: set => myclass_set
procedure :: get => myclass_get
procedure :: get => myclass_get
procedure :: getf => myclass_getf
procedure :: getf2 => myclass_getf2
final :: myclass_del
end type myclass_type

Expand Down Expand Up @@ -38,6 +40,25 @@ subroutine myclass_get(this, array)
print *, 'get method was called'
end subroutine myclass_get

function myclass_getf(this) result(array)
class(myclass_type) :: this
integer, allocatable :: array(:)
! allocate and copy
allocate(array, source=this%arr)
print *, 'getf method was called'
end function myclass_getf

function myclass_getf2(this) result(array)
class(myclass_type) :: this
integer, allocatable :: array(:)
integer :: ier
! sliently deallocate array, even if it is already deallocated
deallocate(array, stat=ier)
! this will automatically allocate array, and then copy
array = this%arr
print *, 'getf method was called'
end function myclass_getf2

subroutine myclass_del(this)
implicit none
type(myclass_type), intent(inout) :: this
Expand Down Expand Up @@ -68,6 +89,10 @@ subroutine test()

call mc%get(vals2)

! function
vals2 = mc%getf()
vals2 = mc%getf2()

! mc will be destroyed when going out of scope
end subroutine

Expand Down

0 comments on commit 3565c0b

Please sign in to comment.