Hello fortran-lang community,
I need to compare polymorphic entities (a==b
) a lot and I always struggle writing a lot of bloatware that basically just replicates the intrinsic comparison operator. Here’s the easiest way I came up with to accomplish that:
type, abstract :: base
contains
procedure(equality), deferred, private :: is_equal
generic :: operator(==) => is_equal
end type base
type, extends(base) :: ext
contains
procedure :: ext_is_equal => is_equal
end type
abstract interface
elemental logical function equality(this,that)
import base
class(base), intent(in) :: this, that
end function equality
end interface
! For each extended type, need to write this one
elemental logical function ext_is_equal(this,that)
class(ext), intent(in) :: this
class(base), intent(in) :: that
select type (that_type => that)
type is (ext)
ext_is_equal = that_type == this ! use intrinsic comparison
class default
ext_is_equal = .false.
end select
end function ext_is_equal
Now I need to write that such routine for each and every extended class, just to use the intrinsic operator. Is this the easiest strategy, or am I missing something?
I would like e.g. to make usage of same_type_as(a,b)
but I can’t because it’s not in a select type
block.