I just encountered the same problem, and I’m wondering if F2023 (see reference below) can solve this situation using the new form of conditional expression?
subroutine optnl_array(array)
integer, intent(in), optional :: array(10)
call optnl_scalar(present(array) ? array(5) : .nil.) ! Is this legal?
end subroutine optnl_array
subroutine optnl_scalar(s)
integer, intent(in), optional :: s
if (present(s)) then
print *, 'S is present, ',s
else
print *, 'S not present'
endif
end subroutine
References:
- The new features of Fortran 2023 | John Reid (PDF, 276 KB)