我该如何在Fortran 90中实现这个目标?我有一个接受函数的例程。
现在,如果我的
因为你无法声明l_mysub。有没有一些我不知道的技巧可以做到这一点?是的,当然我可以做。
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
但我的情况更为复杂,我需要在每个地方都进行这个检查。考虑到我有三个可选的子程序可以传递。