fortran90 - Optional subroutines in Fortran 90 -
how can achieve objective in fortran 90 ? have routine accepting function
subroutine foo(bar, mysub) integer, intent(in) :: bar interface subroutine mysub(x) integer :: x end subroutine end interface call mysub(bar) end subroutine
now want routine 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 standard variable var
like
if (present(var)) l_var = var else l_var = <default value> endif
but far know, cannot perform same optional subroutine. in practice 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)) l_mysub = mysub else l_mysub = default endif call mysub(bar) end subroutine
because cannot declare l_mysub. possible through trick not aware of ? yes, of course can
if (present(mysub)) call mysub(bar) else call default(bar) endif
but case more complex , have put check everywhere. consider have 3 optional subroutines may pass.
my first thought use procedure pointer, noticed specified fortran 90, that's not option.
how making wrapper subroutine original foo
, calls given subroutine if specified, or else default
? (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)) call foo(bar, mysub) else call foo(bar, default) endif end subroutine foo_wrap
with multiple optional subroutines might become little complex, not impossible, think.
Comments
Post a Comment