Search code examples
fortranderived-typesallocatable-array

Do I need an explicit interface to allocate a component of a derived type in a subroutine?


I have a derived type:

module foo
  type bar
      integer, allocatable, dimension(:) :: data
  end type bar
end module foo

Now I would like to allocate bar's data within a subroutine without an explicit interface:

program main
  use foo
  type(bar) :: mybar
  call alloc_my_bar(10,mybar)
  print*, mybar
end program

subroutine alloc_my_bar(n,mybar)
  use foo
  type(bar) :: mybar
  integer :: n
  allocate(mybar%data(n))
  mybar%data = 42
end subroutine alloc_my_bar

This seems to work just fine with ifort, but I know that if mybar wasn't part of a user defined type, I would need an explicit interface ... Does putting the allocatable array into a user defined type remove the need for an explicit interface? What version of the fortran standard is this code compatible with (F90, F95, F2003 ... ) if any?


Solution

  • Allocatable components are defined in TR15581 to F95 that was incorporated to the Fortran 2003 standard. You should not need explicit interface for this, just the use association for the type definition should be fine. You are not passing the array, but the structure around it.