Quantcast
Channel: Intel® Software - Intel® Visual Fortran Compiler for Windows*
Viewing all articles
Browse latest Browse all 5691

Equality comparison for unlimited polymorphic variables

$
0
0

Fortran Gurus: say I have two unlimited polymorphic variables (CLASS(*)), and I want to check if they are equal. Does anyone know if there is a better way than having to write nested SELECT TYPE statements for all possible variable types? Basic example below. Assuming I only cared about built-in variables types (integers, etc.) I'd have to write cases for INT8, INT16, INT32, INT64, REAL32, REAL64, REAL128, and CHARACTERS kinds. This seems unsatisfactory to me, but I can't think of any other way to do it.

 

function equal(k1,k2)

implicit none

class(*),intent(in)    :: k1
class(*),intent(in)    :: k2
logical                :: equal

equal = .false.

if (same_type_as(k1,k2)) then

    select type (k1)
    type is (integer)

        select type (k2)
        type is (integer)
            equal = k1 == k2
        end select

    type is (character(len=*))

        select type (k2)
        type is (character(len=*))
            equal = k1 == k2
        end select

    !type is (...), etc.

    end select

end if

end function equal

 


Viewing all articles
Browse latest Browse all 5691

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>