while trying to add some convenience “destructors” to ErrorFx I hit an interesting paradox. Maybe some of you have some insight, why this appeas.
I’d like to add a generic
destroy() subroutine, which can destroy both, type and class instances of a given type. However, according to the compilers I’ve tried, the interface becomes ambiguous, if the signature of two routines only differs in a
class change (see example below). However, if I only add one of the two routines to the interface, the destruction onlys works for one of the cases (either for
type or for
class). But does that not mean, that the signatures of the two routines are not ambiguous?
module test implicit none private public :: base, destroy type :: base end type base ! You must comment out one of the two associations to compile it interface destroy module procedure destroy_type module procedure destroy_class end interface destroy contains subroutine destroy_type(this) type(base), allocatable, intent(inout) :: this if (allocated(this)) then deallocate(this) end if end subroutine destroy_type subroutine destroy_class(this) class(base), allocatable, intent(inout) :: this if (allocated(this)) then deallocate(this) end if end subroutine destroy_class end module test program test_program use test implicit none type(base), allocatable :: inst_type class(base), allocatable :: inst_class allocate(inst_type) inst_class = inst_type ! Whichever subroutine was associated with the `destroy` interface, ! compilation fails here call destroy(inst_type) call destroy(inst_class) end program test_program
Do I overlook something trivial, or is this “paradox” the expected behavior? And is it possible to create a
destroy() interface which works for both cases?