title: Finalisation in FORTRAN 2003
body: <p>According to <a href="http://fortranwiki.org/fortran/show/Fortran+2003+status" rel="nofollow">Fortran Wiki</a> the <strong>intel fortran</strong> compiler version 14 should support finalisation defined in FORTRAN 2003 standard. I tried to use this feature with <strong>ifort 14</strong>, but observed strange behaviour. Following example should show this:</p>
  
  <pre><code>module mtypes
      implicit none
  
      type mytype
          integer, private :: nr
          contains
  
              final :: delete_mytype
              procedure :: print_mytype
      end type
  
      contains
  
  
  !&gt;      \brief Constructs a new mytype
  !!      \return The created mytype
  !&gt;
          function init_mytype(number)
              type(mytype) :: init_mytype
              integer, intent(in) :: number
  !            allocate(init_mytype)
              init_mytype = mytype(number)
              print *, 'init mytype', number
          end function
  
  !&gt;      \brief De-constructs a mytype object
  !&gt;
          subroutine delete_mytype(this)
              type(mytype) :: this   !&lt; The mytype object that will be finalized
              print *, 'Deleted mytype!', this%nr
          end subroutine delete_mytype
  
  !&gt;      \brief Print something from mytype object
  !&gt;
          subroutine print_mytype(this)
              class(mytype) :: this   !&lt; The mytype object that will print something
              print *, 'Print something from mytype!', this%nr
          end subroutine print_mytype
  
  
  end module mtypes
  
  program main
  
      use mtypes
      type(mytype) :: t1, t2
  
      call t1%print_mytype()
      call t2%print_mytype()
  
      t1 = mytype(1)
      call t1%print_mytype()
      t2 = init_mytype(2)
      call t2%print_mytype()
  
  end program main
  </code></pre>
  
  <p>Within this complete example the <code>type mytype</code> is defined that only has one value <code>nr</code>. This type can be created using simple type constructor e.g. <code>mytype(1)</code> or the initialising function <code>init_mytype</code>. Also a subroutine <code>print_mytype</code> is defined that simply prints <code>mytype%nr</code> to stdout. Finally, the <code>final</code> routine <code>delete_mytype</code> should be used for finalisation, although in this example case it only prints some information to stdout.</p>
  
  <p>This example gives following output:</p>
  
  <pre><code> Print something from mytype!           0
   Print something from mytype!           0
   Deleted mytype!           0
   Print something from mytype!           1
   Deleted mytype!          -2
   init mytype           2
   Deleted mytype!           0
   Deleted mytype!           2
   Print something from mytype!           2
  </code></pre>
  
  <ul>
  <li>Line 1: Ok, t1 initialised with default value 0</li>
  <li>Line 2: Ok, t2 initialised with default value 0</li>
  <li>Line 3: Ok, after assignment of new object <code>t1%mytype(1)</code> the old version is deleted</li>
  <li>Line 4: Ok, version with <code>nr = 1</code> is printed</li>
  <li>Line 5: Strange, where does a version with <code>nr=-2</code> come from?</li>
  <li>Line 6: Ok, version with <code>nr = 2</code> is initialised</li>
  <li>Line 7: Ok, after assignment of new object <code>t2 = init_mytype(2)</code> the old version is deleted</li>
  <li>Line 8: Strange, t2 is finalized before call of <code>t2%print_mytype()</code></li>
  <li>Line 9: Strange, t2 is printed after finalisation</li>
  </ul>
  
  <p>Is this strange behviour caused by some ifort bug or is this caused by wrong application of the finalization FORTRAN 2003 feature and I am doing something wrong?</p>