j3-fortran / fortran_proposals

Proposals for the Fortran Standard Committee
178 stars 15 forks source link

proposal for setting default values of optional arguments #137

Closed milancurcic closed 4 years ago

milancurcic commented 4 years ago

First draft of the proposal to set default value for optional arguments (#22).

milancurcic commented 4 years ago

I will need some help linking to the user survey that @sblionel mentioned (I participated in it, and I read the results back then, but now I can't find it anywhere).

I will also need help suggest a specific and formal addition/change to the standard.

sblionel commented 4 years ago

https://isotc.iso.org/livelink/livelink?func=ll&objId=19530634&objAction=Open&viewType=1

jvdp1 commented 4 years ago

@milancurcic thank you for the proposal. The first draft looks good to me.

I will also need help suggest a specific and formal addition/change to the standard.

I am not sure what you mean.

Should the following cases be discussed in the draft:

1) What is the behavior of present?

subroutine foo(dummy)
     integer, intent(in), optional :: dummy = 0
     print*, present(dummy) ! = .true. if dummy provided, .false. otherwise
end subroutine

2) How does dummy behave when it is optional and allocatable?

subroutine foo(dummy)
     integer, allocatable, intent(in), optional :: dummy(:) = [ 0, 0] 
    if ( .not. present(dummy)) then
     allocate(dummy(5))   !I intentionaly used a dim of 5, instead of 2
     dummy = 1
   end if
end subroutine

I think your proposal has no problem when intent(inout || out), optional is used since the argument is not provided

sblionel commented 4 years ago

@jvdp1 Anything and everything we can think of that seems relevant should be discussed - both of those questions are worthy of mention.

My opinions would be:

  1. present() would always return true for an argument with a default.
  2. That combination is not valid. If the dummy is allocatable, the actual must also be.

My position is that a default value has exactly the same meaning as if the caller passed that value as the corresponding actual argument, with all the existing rules applied. Indeed, I would expect a compiler to implement it this way, doing the substitution at the call and not inside the procedure being called.