开发者

Optional subroutines in Fortran 90

How can I achieve this objective in fortran 90 ? I have a routine accepting a function

subroutine foo(bar, mysub)
   i开发者_开发知识库nteger, intent(in) :: bar
   interface
      subroutine mysub(x)
         integer :: x
      end subroutine
   end interface

   call mysub(bar)

end subroutine

Now I want the routine to be optional

subroutine foo(bar, mysub)
   integer, intent(in) :: bar
   interface
      subroutine mysub(x)
         integer :: x
      end subroutine
   end interface
   optional :: mysub

   call mysub(bar)

end subroutine

Now, if mysub were a standard variable var I could do something like

 if (present(var)) then
     l_var = var
 else
     l_var = <default value>
 endif

but as far as I know, I cannot perform the same for an optional subroutine. In practice this is not possible

subroutine foo(bar, mysub)
   integer, intent(in) :: bar
   interface
      subroutine mysub(x)
         integer :: x
      end subroutine
   end interface
   optional :: mysub

   if (present(mysub)) then
       l_mysub = mysub
   else
       l_mysub = default
   endif

   call mysub(bar)

end subroutine

because you cannot declare l_mysub. Is it possible through some trick I am not aware of ? Yes, of course I can do

   if (present(mysub)) then
       call mysub(bar)
   else
       call default(bar)
   endif

but my case is more complex and I would have to put this check everywhere. Consider that I have three optional subroutines I may pass.


My first thought was to use a procedure pointer, but then I noticed you specified fortran 90, so that's not an option.
How about making a wrapper subroutine for your original foo, which calls it with the given subroutine if it is specified, or else with default? Something like this (untested):

subroutine foo_wrap(bar, mysub)
  integer, intent(in) :: bar
  interface
    subroutine mysub(x)
      integer :: x
    end subroutine mysub
  end interface
  optional :: mysub

  if (present(mysub)) then
    call foo(bar, mysub)
  else
    call foo(bar, default)
  endif
end subroutine foo_wrap  

With multiple optional subroutines it might become a little complex, but not impossible, I think.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜