# \`stdlib\` \`string\_type\` \`parameter\` array currently possible?

**URL:** <https://fortran-lang.discourse.group/t/stdlib-string-type-parameter-array-currently-possible/1939>\
**Category:** Help\
**Created:** [September 27, 2021, 6:15pm UTC](https://fortran-lang.discourse.group/t/stdlib-string-type-parameter-array-currently-possible/1939 "2021-09-27T18:15:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![epagone](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/epagone/32/252_2.png) [@epagone](https://fortran-lang.discourse.group/u/epagone)\
**Post date:** [September 27, 2021, 6:15pm UTC](https://fortran-lang.discourse.group/t/stdlib-string-type-parameter-array-currently-possible/1939/1 "2021-09-27T18:15:25Z")

</div>

Do you know if there is a way to currently define a `parameter` array of varying length strings using `stdlib` `string_type`?

The following fails

```fortran
program main
  use stdlib_string_type
  implicit none

  type(string_type), parameter :: str_param_array(*) = [ "foo", &
                                                          "barr", &
                                                          "bazzz", &
                                                          "quuxxx", &
                                                          "corgeee" ]

  print *, str_param_array
end program main

```

For example gfortran says

```auto
app/main.f90:5:64:

    5 | type(string_type), parameter :: str_param_array(*) = [ "foo", &
      | 1
Error: Different CHARACTER lengths (3/4) in array constructor at (1)
compilation terminated due to -fmax-errors=1.
<ERROR> Compilation failed for object " app_main.f90.o "
<ERROR>stopping due to failed compilation
STOP 1

```

On the other hand, with a “normal” `allocatable` array

```fortran
program main
  use stdlib_string_type
  implicit none

  type(string_type), allocatable :: str_param_array(:)

  str_param_array = [string_type("foo"), string_type("barr"), string_type("bazzz"), &
                      string_type("quuxxx"), string_type("corgeee")]
  print *, str_param_array
end program main

```

it works

```auto
$ fpm run 
 foo barr bazzz quuxxx corgeee

```

Is there a way to set a constant `string_type` array with varying length items (i.e. without padding with spaces)?

Thanks.

---

<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:** [September 27, 2021, 6:25pm UTC](https://fortran-lang.discourse.group/t/stdlib-string-type-parameter-array-currently-possible/1939/2 "2021-09-27T18:25:21Z")

</div>

This is exactly the reason I’d like the new `simple` to be equivalent to `constexpr`. Or at least that simple functions be allowed in constant expressions. I want to be able to define `parameter`s of derived types, even if it has allocatable or private components. If one could overload the structure constructor with a `simple` function and it were allowed in constant expressions, then you could.

Also related, if the `iso_varying_string` module had actually been adopted into the standard, that might have been a way to do it too. The `var_str` constructor could have been defined to be allowed in constant expressions, and that would have allowed for arrays of strings with varying lengths.

At present, there is no way to have a `parameter` with a list of strings of different lengths.

---

<div class="post-metadata">

**Author:** ![epagone](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/epagone/32/252_2.png) [@epagone](https://fortran-lang.discourse.group/u/epagone)\
**Post date:** [September 27, 2021, 6:26pm UTC](https://fortran-lang.discourse.group/t/stdlib-string-type-parameter-array-currently-possible/1939/3 "2021-09-27T18:26:51Z")

</div>

☹ thank you

---

<div class="post-metadata">

**Author:** ![alozada](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/alozada/32/298_2.png) [@alozada](https://fortran-lang.discourse.group/u/alozada)\
**Post date:** [September 27, 2021, 6:29pm UTC](https://fortran-lang.discourse.group/t/stdlib-string-type-parameter-array-currently-possible/1939/4 "2021-09-27T18:29:08Z")

</div>

“F2008 Standard does not allow CHARACTER(LEN=\*) in array constructor”

[https://gcc.gnu.org/bugzilla/show\_bug.cgi?id=77506](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77506)

---

<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:** [September 27, 2021, 6:29pm UTC](https://fortran-lang.discourse.group/t/stdlib-string-type-parameter-array-currently-possible/1939/5 "2021-09-27T18:29:16Z")

</div>

There is the possibility of an approximation.

```auto
associate(my_string_list_parameter => [string_type("foo"), string_type("bar"), ...])
  ! my_string_list_parameter is a constant in here
end associate

```

---

<div class="post-metadata">

**Author:** ![epagone](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/epagone/32/252_2.png) [@epagone](https://fortran-lang.discourse.group/u/epagone)\
**Post date:** [September 27, 2021, 6:31pm UTC](https://fortran-lang.discourse.group/t/stdlib-string-type-parameter-array-currently-possible/1939/6 "2021-09-27T18:31:22Z")

</div>

Good suggestion, but unfortunately I need that array as a public, global entity of a module used in several places.

---

<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:** [September 27, 2021, 6:44pm UTC](https://fortran-lang.discourse.group/t/stdlib-string-type-parameter-array-currently-possible/1939/7 "2021-09-27T18:44:31Z")

</div>

In theory, there might be a way to have it initialized before program start. I’ve heard it’s possible in C to execute code before `main`. Declare a static variable in a C file that is “initialized” by calling a private, `bind(c)` procedure in the module, that initializes your “constants”. Make the variables `protected`, instead of public, and then nothing can modify them. It’s hacky, and everybody who ever sees it will be completely baffled, but it should work.

---

<div class="post-metadata">

**Author:** ![epagone](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/epagone/32/252_2.png) [@epagone](https://fortran-lang.discourse.group/u/epagone)\
**Post date:** [September 27, 2021, 6:47pm UTC](https://fortran-lang.discourse.group/t/stdlib-string-type-parameter-array-currently-possible/1939/8 "2021-09-27T18:47:55Z")

</div>

Yes, thanks for the hint, I might consider it. I vaguely remember that this strategy was (maybe) also used somewhere in `stdlib` but the details escape me now.

---

<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:** [September 27, 2021, 6:52pm UTC](https://fortran-lang.discourse.group/t/stdlib-string-type-parameter-array-currently-possible/1939/9 "2021-09-27T18:52:27Z")

</div>

> [@alozada](#):
>
> “F2008 Standard does not allow CHARACTER(LEN=\*) in array constructor”
> 
> [77506 – F2008 Standard does not allow CHARACTER(LEN=\*) in array constructor](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77506)

I think Fortran 2018 does, and both `gfortran -std=f2018` and `ifort -stand:f18` compile a run a code such as

```auto
    character(len=9), parameter :: dayname(7) = [character(len=9) :: &
      'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', &
      'Saturday', 'Sunday' ]
print*,dayname
end

```

for versions

```auto
GNU Fortran (GCC) 12.0.0 20210718
Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on Intel(R) 64, Version 2021.1 Build 20201112_000000

```

---

<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:** [September 27, 2021, 7:21pm UTC](https://fortran-lang.discourse.group/t/stdlib-string-type-parameter-array-currently-possible/1939/10 "2021-09-27T19:21:28Z")

</div>

This is valid code, however, the values in the array do all have length 9. All of the strings in that array are the same length, some of them are just automatically padded with “blanks”. I’ve got an example about why this might not be the **best** idea.

```auto
program main
  use iso_fortran_env, only: character_kinds
  implicit none

  integer, parameter :: c1 = character_kinds(1), c2 = character_kinds(2)
  character(len=5, kind = c1), parameter :: foo1 = "foo"
  character(len=5, kind = c2), parameter :: foo2 = "foo"

  print *, foo1 == foo2
  print *, foo1 == "foo"
  print *, foo2 == "foo"
end program

```

It is not defined whether this program will print T or F for any of the 3 expressions. Specifically, the blank character is only specified to be space for characters of default kind (i.e. the “foo” literals on the last 2 lines). The comparison operators say that the shorter character is padded with blanks to the length of the longer character, but those blanks _could_ be different characters between different kinds. It is also not defined what the order of the kinds are in the `character_kinds` array, so either `c1` or `c2` or neither might be the default kind.

Granted I have not seen a processor that will actually print F for any of those, but it could and would still be considered standards conforming.

---

<div class="post-metadata">

**Author:** ![epagone](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/epagone/32/252_2.png) [@epagone](https://fortran-lang.discourse.group/u/epagone)\
**Post date:** [September 27, 2021, 7:44pm UTC](https://fortran-lang.discourse.group/t/stdlib-string-type-parameter-array-currently-possible/1939/11 "2021-09-27T19:44:04Z")

</div>

I am almost in disbelief that even something as simple as the following is not allowed

```fortran
program main
  use stdlib_string_type
  implicit none

  type(string_type) :: foo = string_type("foo")
  type(string_type) :: bar = string_type("barr")
  type(string_type) :: baz = string_type("bazzz")
  type(string_type) :: quux = string_type("quuxxx")
  type(string_type) :: corge = string_type("corgeee")
  type(string_type) :: str_param_array(5) = [foo, bar, baz, quux, corge]

  print *, str_param_array
end program main

```

Looks like that without an executable statement (that I cannot use in the declarative section of a module) I cannot create the array. Frustrating.

---

<div class="post-metadata">

**Author:** ![awvwgk](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/awvwgk/32/154_2.png) [@awvwgk](https://fortran-lang.discourse.group/u/awvwgk)\
**Post date:** [September 27, 2021, 8:29pm UTC](https://fortran-lang.discourse.group/t/stdlib-string-type-parameter-array-currently-possible/1939/12 "2021-09-27T20:29:42Z")

</div>

Sadly, we can’t have a constructor for the `string_type` that can be used to set a `parameter` value.

One issue is that we are providing a constructor for the `string_type` to overwrite the default one. This is mainly to keep the raw value safe from user access and to help handle some corner cases where unallocated deferred length character variables are passed to the default constructor.

We could make the default constructor accessible by making the raw value public and require users to promise to not touch it under any circumstances, but this won’t allow the usage of the default constructor in a `parameter` initialization, because the component we want to initialize is `allocatable`:

```nohighlight
/home/awvwgk/projects/src/git/stdlib/_build/src/stdlib_string_type.f90:44:52:

   44 | type(string_type), parameter :: a = string_type("test")
      | 1
Error: Invalid initialization expression for ALLOCATABLE component ‘raw’ in structure constructor at (1)

```

Using the user-defined assignment to initialize a `string_type` from a fixed length character value is also not possible because the procedure is user-defined.

Surprisingly, implementing the `string_type` already pushed quite hard on the limits of what is currently possible in Fortran. For example, we don’t have an `assignment(=)` from `string_type` to character variables because this can’t be implemented for both fixed-length and deferred-length characters in a consistent way.

---

<div class="post-metadata">

**Author:** ![epagone](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/epagone/32/252_2.png) [@epagone](https://fortran-lang.discourse.group/u/epagone)\
**Post date:** [September 27, 2021, 8:54pm UTC](https://fortran-lang.discourse.group/t/stdlib-string-type-parameter-array-currently-possible/1939/13 "2021-09-27T20:54:05Z")

</div>

Just to make it crystal clear: I am very grateful for the excellent work on `string_type` in `stdlib`.

My frustration came from the realisation that the current limitations of the language are more problematic than I imagined. I would dare to say (although I cannot be totally sure) that any major language in use today does not limit the user to this extent for something as trivial as setting a constant list of strings with elements of different length.

---

<div class="post-metadata">

**Author:** ![awvwgk](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/awvwgk/32/154_2.png) [@awvwgk](https://fortran-lang.discourse.group/u/awvwgk)\
**Post date:** [September 27, 2021, 9:46pm UTC](https://fortran-lang.discourse.group/t/stdlib-string-type-parameter-array-currently-possible/1939/14 "2021-09-27T21:46:35Z")

</div>

> [@epagone](#):
>
> My frustration came from the realisation of the current limitations of the language that are more problematic than I imagined.

Fully agree with this statement.

The most important lesson I learned from this is that user-defined `assignment(=)` and automatic reallocation of the LHS don’t work well together:

- the `assignment(=)` is a subroutine and therefore requires explicit declaration of the the LHS
- adding the `allocatable` attribute for the LHS limits the `assignment(=)` to LHS variables with `allocatable` attribute
- leaving out the `allocatable` attribute allows only non-`allocatable` or already allocated LHS variables in the `assignment(=)`
- this scenario is most relevant for deferred-length characters and deferred-length parametrized derived types

I wonder what would be the required change to allow users to define an `assignment(=)` operator that works seamlessly together with automatic LHS reallocation.

There are a couple of other things that bring interesting complications with implementing a fundamental data type like a string in Fortran. For example the user defined derived type input and output (UDDTIO) really made a lot of trouble and is still in a half-finished state for `string_type`.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [September 27, 2021, 10:27pm UTC](https://fortran-lang.discourse.group/t/stdlib-string-type-parameter-array-currently-possible/1939/15 "2021-09-27T22:27:53Z")

</div>

All this pain with `string_type` stems from the fact that Fortran has not been designed to be able to implement user types that behave like intrinsic types, or at least work consistently with other language features such as `=`.

> [@awvwgk](#):
>
> This is mainly to keep the raw value safe from user access and to help handle some corner cases where unallocated deferred length character variables are passed to the default constructor.

If we expose the internal string, wouldn’t that fix the problem? I thought you can have an array of different lengths strings as long as you keep the (allocatable) string as a component of a derived type and I thought that is precisely what `string_type` is for… So I am confused.

---

<div class="post-metadata">

**Author:** ![awvwgk](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/awvwgk/32/154_2.png) [@awvwgk](https://fortran-lang.discourse.group/u/awvwgk)\
**Post date:** [September 27, 2021, 10:39pm UTC](https://fortran-lang.discourse.group/t/stdlib-string-type-parameter-array-currently-possible/1939/16 "2021-09-27T22:39:16Z")

</div>

> [@certik](#):
>
> If we expose the internal string, wouldn’t that fix the problem? I thought you can have an array of different lengths strings as long as you keep the (allocatable) string as a component of a derived type and I thought that is precisely what `string_type` is for… So I am confused.

Making the internal representation public creates some interesting aliasing issues for at least the `assignment(=)` operator:

```fortran
type(string_type) :: str
str = str%raw

```

Exposing the raw representation allows to hack around some issues, but it also introduces new ones. In particular, we lose the memory safety introduced by `string_type` as the raw value can always be unallocated.

Also, for the specific use case of using the default constructor, the `allocatable` attribute is the actual problem, which can’t be mended by making the raw representation public.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [September 28, 2021, 4:18am UTC](https://fortran-lang.discourse.group/t/stdlib-string-type-parameter-array-currently-possible/1939/17 "2021-09-28T04:18:06Z")

</div>

If it is without a constructor, would it then allow to create an array of strings? I still don’t understand what feature that we implemented blocks this use case. Yes, it might be that we will have to choose which nice features we can keep and which we cannot.

---

<div class="post-metadata">

**Author:** ![awvwgk](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/awvwgk/32/154_2.png) [@awvwgk](https://fortran-lang.discourse.group/u/awvwgk)\
**Post date:** [September 28, 2021, 6:32am UTC](https://fortran-lang.discourse.group/t/stdlib-string-type-parameter-array-currently-possible/1939/18 "2021-09-28T06:32:04Z")

</div>

> [@certik](#):
>
> If it is without a constructor, would it then allow to create an array of strings? I still don’t understand what feature that we implemented blocks this use case.

Still doesn’t work for parameter initialization as I explained here:

> [@awvwgk](#):
>
> We could make the default constructor accessible by making the raw value public and require users to promise to not touch it under any circumstances, but this won’t allow the usage of the default constructor in a `parameter` initialization, because the component we want to initialize is `allocatable` :
> 
> ```auto
> /home/awvwgk/projects/src/git/stdlib/_build/src/stdlib_string_type.f90:44:52:
> 
> 44 | type(string_type), parameter :: a = string_type("test")
> | 1
> Error: Invalid initialization expression for ALLOCATABLE component ‘raw’ in 
> 
> ```

It’s a limitation of the Standard not out implementation which disallows using it to initialize parameters. So far there seems to be no hack which we could use to work around the Standard limitations here.

---

<div class="post-metadata">

**Author:** ![epagone](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/epagone/32/252_2.png) [@epagone](https://fortran-lang.discourse.group/u/epagone)\
**Post date:** [September 28, 2021, 10:34am UTC](https://fortran-lang.discourse.group/t/stdlib-string-type-parameter-array-currently-possible/1939/19 "2021-09-28T10:34:36Z")

</div>

> [@awvwgk](#):
>
> Exposing the raw representation allows to hack around some issues, but it also introduces new ones. In particular, we lose the memory safety introduced by `string_type` as the raw value can always be unallocated.

Yes, you are completely correct. I suspect that this is exactly what happened when Stefano Zaghi [made public the inner allocatable in StringiFor following my request](https://github.com/szaghi/StringiFor/issues/28) and that introduced regressions like [this one](https://github.com/szaghi/StringiFor/issues/30).

I believe that there are so many custom-made Fortran implementations of string arrays of varying length that at least might be possible to identify and pick the best design choices or, I’d better say, the less worse considering the limitations of the language.

---

<div class="post-metadata">

**Author:** ![awvwgk](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/awvwgk/32/154_2.png) [@awvwgk](https://fortran-lang.discourse.group/u/awvwgk)\
**Post date:** [September 28, 2021, 10:52am UTC](https://fortran-lang.discourse.group/t/stdlib-string-type-parameter-array-currently-possible/1939/20 "2021-09-28T10:52:51Z")

</div>

> [@epagone](#):
>
> I believe that there are so many custom-made Fortran implementations of string arrays of varying length that at least might be possible to identify and pick the best design choices or, I’d better say, the less worse considering the limitations of the language.

I should mention that I proposed a `string_class` for stdlib as well some time ago:

> <https://github.com/fortran-lang/stdlib/pull/333>
>
> This patch implements an abstract base class (ABC) for an extendible string clas…s (not to be confused with the non-extendible string type). The implementation also shows the full insanity of object oriented string handling in Fortran required to just support the intrinsic functionality and sheds some light on the current state of compilers implementing class inheritance.
> 
> \#### Goals of this PR
> 
> \- \[x\] provide full compatibility with the functional \`string\_type\`
> \- \[x\] provide overloaded interfaces in the base class to define all character intrinsic functions for both character scalars and functional strings
> \- \[x\] add preliminary specs on the public API of the abstract base class
> \- \[\] add developer API documentation for all available type bound procedures
> \- \[x\] add tests to check a minimal implementation of a string class
> 
> \#### Design strategy
> 
> \- provide an ABC rather than an actual class to handle extendible string objects
> \- rely on the \`string\_type\` as return value rather than returning polymorphic string objects
> \- \`stdlib\_string\_class\` defines and exports overloaded interfaces for the ABC, which should be reexported in implementations to provide intrinsic functionality (still requires explicit import)
> \- operators and UDDTIO are type-bound rather than overloaded interfaces (easier to import)
> \- \`string\_class\` can be created by assignment from character values, string types or string objects
> 
> \#### Additional resources
> 
> I prepared the \`stdlib\_string\_class\` module in \[awvwgk/stdlib\_string\](https://github.com/awvwgk/stdlib\_string) as fpm project. The specs are available at https://awvwgk.github.io/stdlib\_string. The projects also includes a version of \[ftlString\](https://github.com/SCM-NV/ftl) and \[StringiFor\](https://github.com/szaghi/StringiFor) based on the \`string\_class\` proposed here.

This implementation did allow libraries like `stringiFor` or `ftlString` to inherit from the same base class and subsequently make the seamlessly intercompatible with each other and stdlib’s `string_type`. But we dismissed the idea in favor of the `string_type` at this time. My repository with the implementation should be still around.

[Next page](https://fortran-lang.discourse.group/t/stdlib-string-type-parameter-array-currently-possible/1939.md?page=2)
