Skip to content

proposal for setting default values of optional arguments #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 11, 2020

Conversation

milancurcic
Copy link
Member

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

@milancurcic
Copy link
Member Author

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
Copy link
Member

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

@jvdp1
Copy link
Contributor

jvdp1 commented Jan 11, 2020

@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
  1. 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 sblionel merged commit 62282eb into j3-fortran:master Jan 11, 2020
@sblionel
Copy link
Member

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants