# Parameterized derived types

**URL:** <https://fortran-lang.discourse.group/t/parameterized-derived-types/3263>\
**Category:** Help\
**Created:** [April 18, 2022, 7:48pm UTC](https://fortran-lang.discourse.group/t/parameterized-derived-types/3263 "2022-04-18T19:48:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![james3](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@james3](https://fortran-lang.discourse.group/u/james3)\
**Post date:** [April 18, 2022, 7:48pm UTC](https://fortran-lang.discourse.group/t/parameterized-derived-types/3263/1 "2022-04-18T19:48:46Z")

</div>

I was attempting something like the following, where I am initializing a parameterized derived type

```auto
module test

implicit none

type x_t(n)
    integer, len :: n
    integer :: v(n)
    character(len = 8), dimension(n) :: str
end type x_t

type y_t
    type(x_t(1)) :: a
    type(x_t(2)) :: b
end type y_t

contains

    ! ...

end module test

program main
    use test
    implicit none
    type(x_t(1)), parameter :: a = x_t(1)([1], [character(len = 8) :: "a1"])
    type(x_t(2)), parameter :: b = x_t(2)([1, 2], [character(len = 8) :: "b1", "b2"])
    type(y_t), parameter :: y = y_t(a, b)

    print *, a
    print *, b
    print *, y

end program main

```

Should this work? In `ifort` I get an internal compiler error

```auto
Internal error store_pdtlen_data_space: Cannot find record
problem.f90: catastrophic error: **Internal compiler error: internal abort** Please report this error along with the circumstances in which it occurred in a Software Problem Report. Note: File and line given may not be explicit cause of this error.

```

In `gfortran` I get errors beginning with

```auto

   25 | type(x_t(1)), parameter :: a = x_t(1)([1], [character(len = 8) :: "a1"])
      | 1
Error: No initializer for component ‘v’ given in the structure constructor at (1)

```

Is there a problem with my syntax, or are these compiler issues?

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [April 18, 2022, 8:55pm UTC](https://fortran-lang.discourse.group/t/parameterized-derived-types/3263/2 "2022-04-18T20:55:39Z")

</div>

The following runs with `ifort`

```fortran
module test

implicit none
public

type x_t(n)
    integer, len :: n
    integer :: v(n)
    character(len=8) :: str(n)
end type x_t

type y_t
    type(x_t(n=1)) :: a
    type(x_t(n=2)) :: b
end type y_t

end module test

program main

    use test
    implicit none
    
    type(x_t(n=1)) :: a
    type(x_t(n=2)) :: b
    type(y_t) :: y

    a = x_t(n=1)([1], [character(len=8) :: "a1"])
    b = x_t(n=2)([1, 2], [character(len=8) :: "b1", "b2"])

    y = y_t(a, b)

    print *, a
    print *, b
    print *, y

end program main

```

Here’s the output:

```nohighlight
ivan@maxwell:~/fortran/20220418_discourse_3263_pdt$ ifort -warn all main.f90 
ivan@maxwell:~/fortran/20220418_discourse_3263_pdt$ ./a.out
           1 a1      
           1 2 b1 a1      
           1 a1 1 2 b1 a1            

```

I have no clue if it should also work in the parameter case. I was surprised to see the structure constructor of the form `x_t(n=1)( ... )` work to begin with. One workaround if you really need the `parameter` attribute could be to abandon the PDT, and simply use parameter arrays of `integer` and `character` string type.

Interestingly, the new Intel Fortran compiler `ifx` recognizes it as a not yet supported language feature:

```nohighlight
$ ifx -warn all main.f90 
/opt/intel/oneapi/compiler/2022.0.2/linux/bin-llvm/xfortcom[0xde27a9]

main.f90(28): error #5533: Feature found on this line is not yet supported in ifx 
    a = x_t(n=1)([1], [character(len=8) :: "a1"])
--------^
compilation aborted for main.f90 (code 3)

```

Hopefully, full support will be added soon.

Concerning `gfortran`, support of parameterized derived types is it’s Achilles’ heel currently. Hopefully it will be improved in the future.

---

<div class="post-metadata">

**Author:** ![nncarlson](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/nncarlson/32/1175_2.png) [@nncarlson](https://fortran-lang.discourse.group/u/nncarlson)\
**Post date:** [April 18, 2022, 9:09pm UTC](https://fortran-lang.discourse.group/t/parameterized-derived-types/3263/3 "2022-04-18T21:09:38Z")

</div>

> [@james3](#):
>
> In `gfortran` I get errors beginning with
> 
> ```auto
> 25 | type(x_t(1)), parameter :: a = x_t(1)([1], [character(len = 8) :: "a1"])
> | 1
> Error: No initializer for component ‘v’ given in the structure constructor at (1)
> 
> ```
> 
> Is there a problem with my syntax, or are these compiler issues?

See [84120 – Syntax for used for PDT constructors is incorrect](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84120)

---

<div class="post-metadata">

**Author:** ![james3](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@james3](https://fortran-lang.discourse.group/u/james3)\
**Post date:** [April 19, 2022, 12:29am UTC](https://fortran-lang.discourse.group/t/parameterized-derived-types/3263/4 "2022-04-19T00:29:57Z")

</div>

Thanks. I was able to test my example using `nagfor` and that works properly, but I think it will be wise for me to avoid using parameterized derived types for now.

---

<div class="post-metadata">

**Author:** ![Harper](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@Harper](https://fortran-lang.discourse.group/u/Harper)\
**Post date:** [April 19, 2022, 5:54am UTC](https://fortran-lang.discourse.group/t/parameterized-derived-types/3263/5 "2022-04-19T05:54:47Z")

</div>

Internal compiler errors should always be reported to the compiler vendor. If your program was valid it should have been compiled. If not, the compiler should have told you about its bugs. (I have reported various internal compiler errors over the years to various vendors. They appreciate it if you can send them a very short program that produces the error. )
