Closed
Description
An annoyance with optional arguments is handling their default values. This usually looks something like:
function mylog(x, base) result(y)
real, intent(in) :: x
real, intent(in), optional :: base
real :: y
real :: base_
base_ = 10.0
if (present(base)) base_ = base
y = log(x)/log(base_)
end function mylog
I propose to introduce a module that exports a generic function default
which will in many cases allow for the elimination of the local copy of the optional argument. The above example could be rewritten
function mylog(x, base) result(y)
use default_values, only: default
real, intent(in) :: x
real, intent(in), optional :: base
real :: y
y = log(x)/log(default(10.0, base))
end function mylog
This is a convenience, but it's incredibly handy. The module is very simple to write. See, e.g., this CLF post by Beliavsky, where I first learned of this trick. I put this in all my serious codes, as it removes a lot of the tedium and error potential with optional arguments.
I can easily spin up a illustrative PR that can be fleshed out once we've decided how we're going to automate generic interfaces.