[J3] Interp: Finalization order for complex objects

Ondřej Čertík ondrej at certik.us
Thu Feb 27 01:38:42 EST 2020


Hi,

We have a question for interpretation at: https://github.com/j3-fortran/fortran_proposals/issues/146. To summarize, the following code makes two compilers to print

1. "CABP" or "CBAP" (both valid)

but a third compiler prints

2. "CBPA". 

Which is the correct answer, 1. or 2.?

Note that there is a similar interpretation paper already submitted: https://j3-fortran.org/doc/year/20/20-117.txt, but the code is slightly different.

Ondrej

-------------------

module child_type

  implicit none
  private

  type :: objectA
  contains
    final :: finalize_objectA
  end type

  type :: objectB
  contains
    final :: finalize_objectB
  end type

  type :: parent
  contains
    final :: finalize_parent
  end type

  type, extends(parent), public :: child
    type(objectA), allocatable :: A
    type(objectB) :: B
  contains
    procedure :: init
    final :: finalize_child
  end type

contains

  subroutine finalize_objectA(this)
    type(objectA), intent(inout) :: this
    write(*,'("A")',advance='no')
  end subroutine

  subroutine finalize_objectB(this)
    type(objectB), intent(inout) :: this
    write(*,'("B")',advance='no')
  end subroutine

  subroutine finalize_parent(this)
    type(parent), intent(inout) :: this
    write(*,'("P")',advance='no')
  end subroutine

  subroutine finalize_child(this)
    type(child), intent(inout) :: this
    write(*,'("C")',advance='no')
  end subroutine

  subroutine init(this)
    class(child), intent(inout) :: this
    allocate(this%A)
  end subroutine

end module

program main
  use child_type
  call run
contains
  subroutine run
    type(child) :: c
    call c%init
  end subroutine
end program


More information about the J3 mailing list