Quantcast
Channel: Intel® Fortran Compiler
Viewing all articles
Browse latest Browse all 3270

generic operator with class(*) behavior question

$
0
0

Hi,

I am wondering how does the generic operator works in case of an abstract class with a class(*) argument. The following code show an example, and the operator on the abstract class do not call the defined operator of intb even though the dynamic type is intb.  Any thought on such design ? A solution will be to override lower_than in intb like that rocedure,private :: lower_than => lowerthan_intb with a class(*) and then do a select type. But i thought that if the dynamic type was intb then the generic function will find lowerthan_intb.

 

 

module tata
implicit none

type,abstract :: b_type
        class(*),allocatable :: b
        contains
                procedure,private :: lower_than
                generic,public :: operator(.lt.) => lower_than
end type

        type,extends(b_type) :: intb
        contains
                procedure,private :: lowerthan_intb
                generic,public :: operator(.lt.) => lowerthan_intb
        end type
        interface intb
                module procedure :: intb_constructor
        end interface
contains
function intb_constructor(i) result(a)
        type(intb) :: a
        integer :: i
        allocate(a%b,source=i)
end function
function lowerthan_intb(a,b) result(r)
        class(intb),intent(in) :: a,b
        logical :: r
        print *,'lowerthan intb'
        select type(i1 => a%b)
        type is(integer)
                select type(i2 => b%b)
                type is(integer)
                        r=i1<i2
                end select
        end select
end function
function lower_than(a,b)
        class(b_type),intent(in) :: a
        class(*),intent(in) :: b
        logical :: lower_than
        lower_than=.false.
        print *, 'operator lower than class* no implementation'
end function


end module

program foo
use tata
implicit none
type(intb) :: d,d2
class(intb),allocatable :: cd,cd2
class(b_type),allocatable :: b1,b2

d=intb(1)
d2=intb(3)
allocate(cd,source=d)
allocate(cd2,source=d2)
print *,d<d2
allocate(b1,source=cd)
allocate(b2,source=cd2)
print *,'b1 extends btype to intb',extends_type_of(b1,d),same_type_as(b1,d)
print *,b1.lt.b2
print *,b1<b2
print *, cd<b1


end program 

 


Viewing all articles
Browse latest Browse all 3270

Trending Articles