Skip to content

Default value for allocatable derived type component #39

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

Open
jacobwilliams opened this issue Oct 23, 2019 · 5 comments
Open

Default value for allocatable derived type component #39

jacobwilliams opened this issue Oct 23, 2019 · 5 comments
Labels
Clause 8 Standard Clause 8: Attribute declarations and specifications

Comments

@jacobwilliams
Copy link

Allow for the default state of allocatable variables to be allocated with a specified value:

type my_type
  integer :: i = 1  ! this has "always" been possible
  character(len=:),allocatable :: str = 'default'  ! this is not currently possible
end type my_type

I'd also like to be able to define parameters with allocatable components like so:

  type(my_type),parameter :: t1 = my_type(1,'option 1')
  type(my_type),parameter :: t2 = my_type(2,'option 2')
  type(my_type),dimension(2),parameter :: list_of_ts = [t1,t2]
@certik
Copy link
Member

certik commented Oct 23, 2019

@jacobwilliams thanks for reporting it. This is related to #22 a bit. I thought that when you write integer :: i = 1, it is setting the save attribute automatically, which is rarely what you want.

@aradi
Copy link
Contributor

aradi commented Oct 23, 2019

I think, as long as it is an assignment in a derived type declaration, it does not have any direct implications for the save attribute of eventual instances of it. However, the spirit of this kind of assignment could be also carried over to proposal #22 by allowing default values for optional allocatable dummy arguments as well.

@milancurcic
Copy link
Member

milancurcic commented Oct 23, 2019

Implicit save behavior is relevant only in procedures.

However, now I realize that in the context of default value for derived type components, which I've been happily using for years, the type constructor uses the same style as proposed in #22. For example:

type my_type
  integer :: i = 1  
end type my_type

type(my_type) :: t1 = my_type()  ! this works, t1 % i is 1
type(my_type) :: t2 = my_type(3) ! this works also, t2 % i is 3

So, the proposal #22 (sorry to hijack the thread :)) is consistent with the current syntax for default values of derived type components, except there we need an extra attribute (default) to work around the implicit save behavior.

I like this proposal also. It could be merged with #22, or have two separate smaller proposals. We should make whatever is easier to digest by the committee.

@jacobwilliams
Copy link
Author

Frankly, I think the implicit save behavior should be removed. It is a malicious anti-feature of the language. But, of course it will be be with us until the end of time (like implicit typing I suppose).

@certik
Copy link
Member

certik commented Oct 23, 2019

@jacobwilliams I created #40 for removing the implicit save behavior.

@certik certik added the Clause 8 Standard Clause 8: Attribute declarations and specifications label Apr 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Clause 8 Standard Clause 8: Attribute declarations and specifications
Projects
None yet
Development

No branches or pull requests

4 participants