# Allocate an allocatable character variable along with other data types

**URL:** <https://fortran-lang.discourse.group/t/allocate-an-allocatable-character-variable-along-with-other-data-types/2387>\
**Category:** Help\
**Created:** [December 9, 2021, 9:36pm UTC](https://fortran-lang.discourse.group/t/allocate-an-allocatable-character-variable-along-with-other-data-types/2387 "2021-12-09T21:36:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [December 9, 2021, 9:36pm UTC](https://fortran-lang.discourse.group/t/allocate-an-allocatable-character-variable-along-with-other-data-types/2387/1 "2021-12-09T21:36:36Z")

</div>

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?)

```auto
program main
implicit none
character (len=:), allocatable :: word
real, allocatable :: x(:)
integer, allocatable :: ivec(:)
allocate (character (len=5) :: word)
allocate (x(2),ivec(2))
allocate (x(2), character (len=5) :: word) ! not legal
end program main

```

---

<div class="post-metadata">

**Author:** ![sblionel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/sblionel/32/853_2.png) [@sblionel](https://fortran-lang.discourse.group/u/sblionel)\
**Post date:** [December 10, 2021, 7:37pm UTC](https://fortran-lang.discourse.group/t/allocate-an-allocatable-character-variable-along-with-other-data-types/2387/2 "2021-12-10T19:37:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [December 11, 2021, 12:09am UTC](https://fortran-lang.discourse.group/t/allocate-an-allocatable-character-variable-along-with-other-data-types/2387/3 "2021-12-11T00:09:23Z")

</div>

I think there may be some syntactic ambiguities that make describing the feature, and detecting erroneous code a bit tricky. For example

```auto
class(base), allocatable :: foo, bar
...
allocate(type(child) :: foo, bar)

```

Is this allocating both `foo` and `bar` with dynamic type `child`? Or (assuming `base` isn’t `abstract`) does `bar` have dynamic type `base`?

```auto
class(base), allocatable :: foo
type(unrelated), allocatable :: bar
...
allocate(type(child) :: child, bar)

```

Is this allowed?

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [December 11, 2021, 1:15am UTC](https://fortran-lang.discourse.group/t/allocate-an-allocatable-character-variable-along-with-other-data-types/2387/4 "2021-12-11T01:15:30Z")

</div>

> [@everythingfunctional](#):
>
> I think there may be some syntactic ambiguities that make describing the feature, and detecting erroneous code a bit tricky. For example
> 
> ```auto
> class(base), allocatable :: foo, bar
> ...
> allocate(type(child) :: foo, bar)
> 
> ```
> 
> Is this allocating both `foo` and `bar` with dynamic type `child` ? Or (assuming `base` isn’t `abstract` ) does `bar` have dynamic type `base` ?
> 
> ```auto
> class(base), allocatable :: foo
> type(unrelated), allocatable :: bar
> ...
> allocate(type(child) :: child, bar)
> 
> ```
> 
> Is this allowed?

Note first there is an error in syntax: the standard oddly enough does not require nor permit the `type`-based declaration in a `type-spec` and instead asks users to go bare:

```Fortran
   allocate( child :: foo, bar )

```

With this syntax correction then, the answer to " Is this allocating both `foo` and `bar` with dynamic type `child` ?" is yes per the standard, it is allowed.

In the second case, I assume what you meant to show was

```Fortran
class(base), allocatable :: foo
type(unrelated), allocatable :: bar
...
allocate( child :: foo, bar ) !<-- i.e., not child, bar.

```

And in this case, one of the constraints in the `ALLOCATE` statement section of the standard re: type-compatibility should kick in and require a processor to detect and report it.

---

<div class="post-metadata">

**Author:** ![urbanjost](https://avatars.discourse-cdn.com/v4/letter/u/0ea827/32.png) [@urbanjost](https://fortran-lang.discourse.group/u/urbanjost)\
**Post date:** [December 11, 2021, 2:52am UTC](https://fortran-lang.discourse.group/t/allocate-an-allocatable-character-variable-along-with-other-data-types/2387/5 "2021-12-11T02:52:15Z")

</div>

It is probably too late, but instead of the “NAME\_OF\_TYPE :: list” syntax, a functional syntax like

```fortran
allocate(foo(len=10,dimension=4),bar(len=30,dimension=40),....) 

```

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.

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [December 13, 2021, 4:00pm UTC](https://fortran-lang.discourse.group/t/allocate-an-allocatable-character-variable-along-with-other-data-types/2387/6 "2021-12-13T16:00:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [December 13, 2021, 4:30pm UTC](https://fortran-lang.discourse.group/t/allocate-an-allocatable-character-variable-along-with-other-data-types/2387/7 "2021-12-13T16:30:13Z")

</div>

Can parentheses be required to avoid ambiguity? For example

`allocate ((character (len=5) :: w1, w2), (character (len=1) :: a), x(2))`

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [December 13, 2021, 5:17pm UTC](https://fortran-lang.discourse.group/t/allocate-an-allocatable-character-variable-along-with-other-data-types/2387/9 "2021-12-13T17:17:30Z")

</div>

> [@kargl](#):
>
> The place to start would be to grab 21-007r1.pdf and look at the current syntax.  
> Why complicate what is already a clean straight forward design? Does  
> type-spec `real(kind=dp)` apply to x(2), and does the separation, possibly  
> with continuation lines, make the code easier to understand?
> 
> `allocate (real(kind=dp) :: (character (len=5) :: w1, w2), (character (len=1) :: a), x(2))`

The type-spec would be required to be just before the variables it pertains to. One would write

`allocate ((real(kind=dp) :: x(2)), (character (len=5) :: w1, w2), (character (len=1) :: a))`
