Fortran-202X-Proposals icon indicating copy to clipboard operation
Fortran-202X-Proposals copied to clipboard

Proposal: default value for optional dummy arguments

Open szaghi opened this issue 8 years ago • 5 comments

Allow the definition of default value for optional dummy arguments:

subroutine foo(bar, baz=.false.) ! inline default value definition
integer, intent(inout)        :: bar
logical, intent(in), optional :: baz

if (baz) then ! using baz even when it is not passed is good because it has a default value
   bar = bar + 1
else
   bar = bar - 1
endif
end subroutine foo 

The default value is clearly indicated into the procedure signature. I presume that the default value should follow all the rules that apply to variable initialization. The builtin present will remain untouched as well the other optional related behaviors except that baz is usable even in the case it is not passed when a default value is assigned.

Rationale

Without the ability to define a default value to optional dummy arguments, we have to mimic it by local variables, e.g.

subroutine foo(bar, baz)
integer, intent(inout)        :: bar
logical, intent(in), optional :: baz
logical                       :: baz_local

baz_local = .false.
if (present(baz)) baz_local = baz
if (baz_local) then
   bar = bar + 1
else
   bar = bar - 1
endif
end subroutine foo

that is more verbose and error prone, especially when the optional dummies are many.

Other languages

Python has such a feature.

szaghi avatar Jul 12 '17 14:07 szaghi

It should also work for intent(out) variables.

And what about putting it in-line with the variable declaration?

subroutine foo(bar, baz)
integer, intent(inout)        :: bar
logical, intent(in), optional :: baz=.false.

I like this better, but does it conflict with the stupid "implicit save" thing?

jacobwilliams avatar Jul 12 '17 14:07 jacobwilliams

@jacobwilliams

Good point, I forget about the implicit save rule... maybe it has not any conflicts, the default value looks like a saved value :smile:

Anyhow I am not expert of Standard rules subtles...

szaghi avatar Jul 12 '17 14:07 szaghi

I hate implicit save. It is one of the stupidest "features" in the language (I've only ever seen it used by accident). The sooner it is removed the better (but of course, that will never happen).

jacobwilliams avatar Jul 12 '17 15:07 jacobwilliams

@jacobwilliams

Me too, in general I hate implicit rules that are not intuitive such this one.

szaghi avatar Jul 12 '17 15:07 szaghi

I like this better, but does it conflict with the stupid "implicit save" thing?

Yes, this will def have to be implicit save to be consistent with the rest of the language. I would love meet the jerk who introduced the implicit save feature to the language.

zbeekman avatar Jul 21 '17 21:07 zbeekman