It is convenient to allocate multiple variables in the same statement. I am starting to use allocatable (dynamic length) character variables in my codes and wonder if it possible to allocate them in the same statement as other types of variables. My attempt to do so in the code below, commented ! not legal, did not work. (Would it be possible to extend the language so that it was legal?)
character (len=:), allocatable :: word
real, allocatable :: x(:)
integer, allocatable :: ivec(:)
allocate (character (len=5) :: word)
allocate (x(2), character (len=5) :: word) ! not legal
end program main
I can’t think of an issue that would block this, but some might object to it as “syntactic sugar”. It would complicate the description of ALLOCATE because you’d have to explain how the type was chosen for allocate-objects that don’t have a type specified (think polymorphic).
would have been better in my mind. Having a syntax almost like but not quite like a declaration statement where all the items on the RHS of the “::” have to be the same versus something without the “::” being allowed to be different types seems to have been a complicated solution, in that the behavior becomes so different as to act like two different statements.
Yeah, this is what I was trying to show, and I agree with your assessment. But this doesn’t address the OP’s question of “can we allocate variables of different types in the same allocate statement, where one (or more) of the variables require a type-spec in order to specify a length or kind type parameter?”
Now I don’t think it would be a bad idea to allow for this, but it’s not quite so easy as it might at first appear to design this capability in a way that avoids these pitfalls.