Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Nested Select Type

Dharma
Beginner
584 Views
I seem to get a wrong result on using nested select type. Can you tell me if this code is incorrect or if it is a problem with the compiler (ifort version 12.1.0).

[fortran]module comparator
  implicit none
contains
  function compareLess(A,B) result(comp)
    class(*),pointer, intent(in) :: A
    class(*),pointer, intent(in) :: B
    logical :: comp
    comp=.false.
    select type(C=>A)
      ! Check if Type of A is integer
      type is(integer)
      select type(D=>B)
        type is(integer)
        comp = C.lt.D
      end select
      print*,'Error :: A is type int, B is not type int'
      stop
      ! Check if Type of A is real
      type is(real)
      print*,'Type of A is real',C
      select type(D=>B)
        type is(real)
        comp = C.lt.D
      end select
      print*,'Error :: A is type real, B is not type real'
      stop
      ! Check if Type of A is character
      type is(character(len=*))
      select type(D=>B)
        type is(character(len=*))
        comp = C.lt.D
      end select
      print*,'Error :: A is type character, B is not type character'
      stop
    end select
  end function compareLess
end module comparator
program TestComp
  use comparator
  class(*), pointer :: A
  class(*), pointer :: B
  ! enable to test integer
  integer, pointer :: C
  integer, pointer :: D
  ! enable to test reals
  !real, pointer :: C
  !real, pointer :: D
  logical :: comp
  allocate(C,D)
  ! enable to test reals
  !call random_number(C)
  !call random_number(D)
  C=5
  D=8
  A => C
  B => D
  ! Method 1
  !!---
  ! Code compiles, and runs but the result is,
  ! Error :: A is type int, B is not type int
  !!---
  comp = compareLess(A,B)
  print*,'A is',C,'and B is',D,'A is less than B :',comp
 ! Method 2
 !!---
 ! Code compiles, and throws error,
 ! severe (40): recursive I/O operation, unit -1, file unknown
 ! This may be understanable, I tried method 2 first before i tried method 1.
 ! How does on deal with a situation like this, where one wants to print the result
 ! of compare but also let the compare print an error messge in case of error in compare.
 ! Test for sucess of compare and then print?
 !!---
 ! Enable to test method 2
 !print*,'A is',C,'and B is',D,'A is less than B :',compareLess(A,B)
end program TestComp[/fortran]
0 Kudos
2 Replies
Anonymous66
Valued Contributor I
584 Views

You need to add a return after line 14, line 22, and line 31. Because there is no return of break statement, the program continues and hits the error code after it does the comparison.

0 Kudos
Dharma
Beginner
584 Views
Thanks. I should have paid more attention :)
0 Kudos
Reply