Object-oriented Fortran

Rouson, Damian rouson@sandia.gov
Sat Sep 13 11:42:00 GMT 2008


Tobias,

I think I meant allocatable scalars so long as those scalars can be of derived type.  Basically, I would like to give the 'allocatable' attribute to
1. Dummy arguments that are derived type scalars,
2. Function results that are derived type scalars, and
3. Derived type components that are themselves derived type scalars.
I also of course need to be able to pass derived type scalars as arguments to 'allocate.'  I have found that when I have access to these features along with access to the 'move_alloc' intrinsic procedure, I can eliminate all need for pointers in my applications, which means I can also eliminate all need for final procedures. I see this as one of the most attractive features of object-oriented programming in Fortran 2003.

Just in case, I'm still not using the right terminology, an illustration of items 1 and 2 is below.

Damian

module fooModule
  implicit none
  private        ! Hide everything by default
  type, public :: foo
    private
    integer :: bar=1
  contains
    procedure :: fooSum
    procedure :: fooBar
  end type

contains

  function fooSum(left,right) ! add two foo instances
    type(foo), intent(in) :: left    ! will switch to 'class' when supported
    type(foo), intent(in) :: right
    type(foo),allocatable :: fooSum
    allocate(fooSum)
    fooSum%bar = left%bar + right%bar
  end function

  function fooBar(this)
    type(foo) :: this  ! will switch to 'class' when supported
    integer   :: fooBar
    fooBar = this%bar
  end function
end module

program main
  use fooModule
  implicit none
  type(foo)              :: a,b
  type(foo), allocatable :: c
  allocate(c)
  c = a%fooSum(b) ! switch to 'c = a + b' once supported
  print *,'a=',a%fooBar()
  print *,'b=',b%fooBar()
  print *,'c=',c%fooBar()
end program








More information about the Fortran mailing list