画面に「Finalization」と書き込むだけt_fileのファイナライズルーチンを備えた派生型があります。closeタイプのインスタンスを返す関数もありますt_file。このプログラムの出力は次のとおりです。
Finalization.
Finalization.
Just opened
     2000
Done.
2つの質問があります:
- 出力の前にファイナライズが行われるのはなぜですか?
Just opened - ファイナライズが2回行われるのはなぜですか?
 
私のコンパイラはIntel(R)Visual Fortran Composer XE201112.1.3526.2010です。
コードは次のとおりです。
module m_file
    implicit none
    type t_file
        integer::iu=1000
        contains
        final::close
    end type
    contains
    function openFile() result(f)
        implicit none
        type(t_file)::f
        f%iu = 2000
    end function
    subroutine close(this)
        implicit none
        type(t_file)::this
        write(*,*) 'Finalization.'
    end subroutine
end module
program foo
    use m_file
    implicit none
    type(t_file)::f
    f = openFile()
    write(*,*) 'Just opened'
    write(*,*) f%iu
    write(*,*) 'Done.'    
    read(*,*)
end program