[J3] [EXTERNAL] Re: Default values for optional arguments

Clune, Thomas L. (GSFC-6101) thomas.l.clune at nasa.gov
Fri Jan 22 14:18:10 UTC 2021


Yes – this is the approach I generally take these days with “optional” (intent(in)) arguments.     There is a bit of extra work in setting up the overloaded interface, for which OPTIONAL was intended to spare us.    But the intent is clear, and the actual literals for the default values are in a relatively consistent location.

But again the real problem arises when there are multiple such dummy arguments.   Lots of things look tolerable when there is only one.    But suppose you have just 3 independent “optional,” intent(in) arguments.   You then are overloading a total of 8 interfaces, and the convenience of using one interface with OPTIONAL starts to tempt you back.      For my code this is fortunately rare.  Even when there are multiple “optional” arguments there is usually a natural nesting such that number of interfaces scales linearly with their number rather than factorially.


From: J3 <j3-bounces at mailman.j3-fortran.org> on behalf of Vipul Parekh via J3 <j3 at mailman.j3-fortran.org>
Reply-To: General J3 interest list <j3 at mailman.j3-fortran.org>
Date: Thursday, January 21, 2021 at 10:37 PM
To: General J3 interest list <j3 at mailman.j3-fortran.org>
Cc: Vipul Parekh <parekhvs at gmail.com>
Subject: [EXTERNAL] Re: [J3] Default values for optional arguments


On Wed, Jan 20, 2021 at 2:32 AM Thomas König via J3 <j3 at mailman.j3-fortran.org<mailto:j3 at mailman.j3-fortran.org>> wrote:

A somewhat common thing to do for optional arguments is to set them
to a default value if the caller did not specify anything.  ..

What could help is to add some way to express default initialization for
optional arguments, something along the lines of

   integer, intent(in), optional :: b = 42

or

   integer, intent(in), optional, default (42) :: b

Opinions? ..


I agree with Malcolm in terms of the risk of "syntactic cyanide" with pursuing any of this now considering all the work that has gone into the language since Fortran 90.  And given the introduction of PRESENT starting Fortran 90, there is also the risk of a semantic landmine with doing any of this now.

For the simple example given in the original email, an alternative "idiom" supported by the current standard is the following:
--- begin example ---
module foo_m
   generic :: foo => foo_1, foo_2
contains
   subroutine foo_1( a, b )
      integer :: a
      integer(..), intent(..) :: b ! <-- not optional
      ..
      ! subprogram including instructions that reference 'b'
   end subroutine
   subroutine foo_2( a )
      integer :: a
      call foo_1(a, b=42 )
   end subroutine
end module
--- end example ---

Compared to the "cost" of all work in the standard to introduce "default values" of optional arguments, the above is a relatively inexpensive workaround that is reasonably safe and efficient while maintaining readability  Note there is no code vulnerability here with 'b' versus 'b_val'.

Regards,
Vipul Parekh




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.j3-fortran.org/pipermail/j3/attachments/20210122/e005e42b/attachment.htm>


More information about the J3 mailing list