# Using select type on allocatable character

**URL:** <https://fortran-lang.discourse.group/t/using-select-type-on-allocatable-character/7815>\
**Category:** Help\
**Created:** [April 11, 2024, 9:40am UTC](https://fortran-lang.discourse.group/t/using-select-type-on-allocatable-character/7815 "2024-04-11T09:40:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgunde](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@mgunde](https://fortran-lang.discourse.group/u/mgunde)\
**Post date:** [April 11, 2024, 9:40am UTC](https://fortran-lang.discourse.group/t/using-select-type-on-allocatable-character/7815/1 "2024-04-11T09:40:45Z")

</div>

Hello,  
i am trying to reduce some code by writing a generic routine for setting values to variables of different types, using a `class(*)` argument and performing `select type` on it. The following code works fine for fixed-length strings, but not for `character(:), allocatable`, which does not seem to get written at all in the routine.

```auto
program main

  implicit none
  character(:), allocatable :: alloc_str
  character(len=20) :: fixed_str

  call set_me( fixed_str )
  write(*,*) fixed_str

  call set_me( alloc_str )
  write(*,*) allocated( alloc_str ), alloc_str

contains

  subroutine set_me( val )
    implicit none
    class(*), intent(out) :: val

    select type( val )
    type is( character(*) )
       val = "a string"
    class default
       write(*,*) "unknown type of val"
    end select

  end subroutine set_me

end program main

```

output:

```bash
 a string            
 F 

```

Is there some workaround for this, or a better way of doing it?  
The goal would be to be able to accept and write both, the fixed-length and allocatable strings.

I thought of catching an error with checking if `len_trim(val)` is zero, which would indicate a non-allocated allocatable, but i can’t seem to find any info about the result of `len_trim` for a non-allocated string. Testing on `gfortran` gives zero, but is this true across all the different compilers? (analogous to `size()` of unallocated arrays being sometimes random)

---

<div class="post-metadata">

**Author:** ![PierU](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pieru/32/1848_2.png) [@PierU](https://fortran-lang.discourse.group/u/PierU)\
**Post date:** [April 11, 2024, 10:20am UTC](https://fortran-lang.discourse.group/t/using-select-type-on-allocatable-character/7815/2 "2024-04-11T10:20:18Z")

</div>

> [@mgunde](#):
>
> The goal would be to be able to accept and write both, the fixed-length and allocatable strings.

You can pass an unallocated actual string argument to a dummy string argument that is not declared as `allocatable`, but then you can’t expect it to be allocated at all in the routine.

---

<div class="post-metadata">

**Author:** ![mgunde](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@mgunde](https://fortran-lang.discourse.group/u/mgunde)\
**Post date:** [April 11, 2024, 11:27am UTC](https://fortran-lang.discourse.group/t/using-select-type-on-allocatable-character/7815/3 "2024-04-11T11:27:27Z")

</div>

The code i posted is just the minimal example, sorry. In the full code there are more cases in the `select type`, not only for `character`.

> [@PierU](#):
>
> pass an unallocated actual string argument to a dummy string argument

I am trying to actually avoid having separate routines for each type of variable, if this is what you are suggesting?

---

<div class="post-metadata">

**Author:** ![PierU](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pieru/32/1848_2.png) [@PierU](https://fortran-lang.discourse.group/u/PierU)\
**Post date:** [April 11, 2024, 11:40am UTC](https://fortran-lang.discourse.group/t/using-select-type-on-allocatable-character/7815/4 "2024-04-11T11:40:12Z")

</div>

You can have a single routine, but then the allocatable variables must be allocated befoire calling the routine.

---

<div class="post-metadata">

**Author:** ![mgunde](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@mgunde](https://fortran-lang.discourse.group/u/mgunde)\
**Post date:** [April 11, 2024, 12:02pm UTC](https://fortran-lang.discourse.group/t/using-select-type-on-allocatable-character/7815/5 "2024-04-11T12:02:32Z")

</div>

Ok, so if i understand correctly, for cases where i want to write a value to a variable, it is actually better to write separate routines because then i can manage the allocation in the routine; while for cases where i read a value from a variable i can use a generic routine with `class(*)` since the variable is surely already allocated.

---

<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:** [April 11, 2024, 1:49pm UTC](https://fortran-lang.discourse.group/t/using-select-type-on-allocatable-character/7815/6 "2024-04-11T13:49:18Z")

</div>

> [@mgunde](#):
>
> ` class(*), intent(out) :: val`

> [@PierU](#):
>
> You can pass an unallocated actual string argument to a dummy string argument that is not declared as `allocatable`

It is invalid to pass an unallocated actual argument to a non-allocatable dummy argument.

> [@mgunde](#):
>
> for cases where i want to write a value to a variable

I would caution against where you’re headed with this design. The call site knows the type. You’re making things more difficult by trying to make a single procedure re-determine the type.

---

<div class="post-metadata">

**Author:** ![PierU](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pieru/32/1848_2.png) [@PierU](https://fortran-lang.discourse.group/u/PierU)\
**Post date:** [April 11, 2024, 2:11pm UTC](https://fortran-lang.discourse.group/t/using-select-type-on-allocatable-character/7815/7 "2024-04-11T14:11:44Z")

</div>

> [@everythingfunctional](#):
>
> It is invalid to pass an unallocated actual argument to a non-allocatable dummy argument.

OK… I thought that specifically for the character type it was somehow equivalent to passing a zero length string.

---

<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:** [April 11, 2024, 2:15pm UTC](https://fortran-lang.discourse.group/t/using-select-type-on-allocatable-character/7815/8 "2024-04-11T14:15:56Z")

</div>

> [@PierU](#):
>
> I thought that specifically for the character type it was somehow equivalent to passing a zero length string.

Nope, unallocated and zero length/size are very different things. I.e. my bucket is empty vs I have no bucket. That goes for deferred length character variables as well as arrays.
