fortran66のブログ

fortran について書きます。

select type

万能型 class(*) と select type による処理。F90でせっかく型チェックを厳しくしたのに、OOPのせいでまたゆるゆるにwww

実行結果

ソースプログラム

program test
  implicit none
  class(*), allocatable :: d
  integer :: i
  
  allocate(real(8)::d)
  do i = 1, 3
    select type(d)
      type is (double precision) 
        d = tan(0.5d0 * 355.0d0 / 113.0d0)
        print *, d, 'pi - 355 / 113 =', 4 * atan(1.0d0) - 355.0d0 / 113.0d0
      type is (character(*)) 
        d = 'abcdefg'
        print *, d
      class default
        print *, 'no match'
    end select
      
    deallocate(d)  
    if (i == 1) then 
      allocate(character(len = 3) :: d)
    else
      allocate(real::d)
    end if
  end do

  stop
end program test