Skip to content

Optional applied to groups of arguments #77

@jacobwilliams

Description

@jacobwilliams

I have no idea what the syntax would look like, but I have often wished that we could have some way of checking at compile time the presence/absence of a group of optional arguments (if they are all present or not). The only way currently to do this is at run time.

The use case is to avoid code like this:

subroutine test(x,y,z,opt1,opt2)
...
if (present(opt1) .eqv. present(opt2)) then
   if (present(opt1)) then
     ! both are present ...
   else 
     ! neither are present ...
   end if
else
   error stop 'error: have to use opt1 and opt2 at the same time'
   ! or return some error code...
end if
...

I'd like some way to be able to call a routine like this such as:

   call test(x,y,z)        ! ok
   call test(x,y,z,o1,o2)  ! ok
   call test(x,y,z,o2=o2)  ! this is a compile time error

Overloading multiple functions works for some cases like this, but not all (say you have many variables like this of the same type grouped into different sets that you want to test). Using derived types also works for some cases but may be unsatisfactory or not possible for others.

Just an idea.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Clause 15Standard Clause 15: Procedures

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions