我怎样才能在 fortran 90 中实现这个目标?我有一个接受函数的例程
subroutine foo(bar, mysub)
integer, intent(in) :: bar
interface
subroutine mysub(x)
integer :: x
end subroutine
end interface
call mysub(bar)
end subroutine
现在我希望例程是可选的
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
现在,如果 mysub 是一个标准变量var
,我可以做类似的事情
if (present(var)) then
l_var = var
else
l_var = <default value>
endif
但据我所知,我不能对可选子程序执行相同的操作。在实践中这是不可能的
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
因为你不能声明 l_mysub。是否有可能通过一些我不知道的技巧?是的,我当然可以
if (present(mysub)) then
call mysub(bar)
else
call default(bar)
endif
但我的情况更复杂,我不得不把这张支票放在任何地方。考虑到我可以通过三个可选子例程。