Default initialization and structure constructors

Default initialization and structure constructors

I tried to compile the following code from 'Modern Fortran Explained' (Metcalf et al), p291, with the derived type parameters commented out:

type character_with_max_length!(maxlen, kind)
!integer, len :: maxlen
!integer, kind :: kind = kind(’a’)
integer :: length = 0
character(20) :: value(100)
end type character_with_max_length

type(character_with_max_length) :: name=character_with_max_length('John Hancock')

I get the following compile error:
Omitted field is not initialized. Field initialization missing:   [VALUE]

It appears that with the IVF compiler (2013) all components that are default initialized must come after the non-default initialized to make structure constructors work _without keywords_. {The following compiles: type(character_with_max_length) :: name=character_with_max_length(value='John Hancock') }

Should it be this way according to the standard (and, hence, Metcalf et al have an erroneous example), or is this a bug in the compiler?

4 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

My interpretation is that, even with default initializations, this kind of initialization follows (in a fashion) the same as a subroutine call with optional arguments. That is to say, you would need to repeat those elements in your type in your simple initialization. I more often use the default initializations in conjunction with an Allocate for the Type.

You need to say:

name=character_with_max_length(value='John Hancock')

If you're going to list values without keywords, you have to specify them all in order, even those with default initializations.

Retired 12/31/2016

I just want to ask again to be sure:
Is the example by Metcalf et al wrong (according to the standard)?
In the case of 'yes' on that question, I feel that the potential of default initialization has not been taken full advantage of. As long as the number of arguments in a constructor is the same as tne number of components without default initialization in the type, it should be straight forward to match arguments and components (assuming of course that the types and kinds match) without having to specify any keywords. This could be taken even further to allow for redefinition of components *with* default initialization, as long as it can be determined (at compile time) how to match constructor arguments with type components. Still without keywords. Wouldn't it be much like matching procedures in an generic interface?
I know that I can put all the components with default initialization at the end in the "component list" to be able to declare the rest without using keywords, but IMO that is not preferable if there is some logical connection between the components so that they should be grouped in, say, a WRITE.
Of course, I don't blame you guys at Intel, if this is the rule found in the standard.

Leave a Comment

Please sign in to add a comment. Not a member? Join today