0

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?

Charles
  • 50,943
  • 13
  • 104
  • 142
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • Why not put the subroutine in a module and automatically get an interface? What's the drawback? Whether or not an explicit interface is required, it can help the programmer. – M. S. B. Feb 21 '13 at 08:40
  • @M.S.B - Then you impose compiling order. From a purist perspective, it seems hacky to put a single subroutine in a module. Modules seem like they should be used to group related data/items. In my case, `foo` already has a bunch of subroutines related to it. In reality, my program is structured such that module `foo` has type `bar` and subroutine `alloc_bar` and other subs. Then my code calls some other function (passing in a `bar`), calling `alloc_bar` and then populating the data. The question isn't whether `alloc_bar` will work, but whether the main code will see the change too. – mgilson Feb 21 '13 at 13:30

1 Answers1

2

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.

IanH
  • 21,026
  • 2
  • 37
  • 59