# Polymorphic array in a derived type

**URL:** <https://fortran-lang.discourse.group/t/polymorphic-array-in-a-derived-type/3011>\
**Category:** Help\
**Created:** [March 21, 2022, 5:41pm UTC](https://fortran-lang.discourse.group/t/polymorphic-array-in-a-derived-type/3011 "2022-03-21T17:41:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![St\_Maxwell](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@St\_Maxwell](https://fortran-lang.discourse.group/u/St_Maxwell)\
**Post date:** [March 21, 2022, 5:41pm UTC](https://fortran-lang.discourse.group/t/polymorphic-array-in-a-derived-type/3011/1 "2022-03-21T17:41:16Z")

</div>

Hello everyone, I am writing codes of a data structure, and surprisingly they do not compile with gfortran. It seems that gfortran just crashes and does not print error information. But when using ifort, the codes can successfully compile and produce expected results. The codes below are simplified from original codes as a minimal case.

```fortran
module polymorphic_array
    implicit none
    
    type :: base_wrapper
        class(base), allocatable :: val
    end type

    type :: base
        character(len=10) :: name
        type(base_wrapper), allocatable :: list(:)
    end type

    type, extends(base) :: child
    end type

end module polymorphic_array

program main
    use polymorphic_array
    implicit none
    type(child) :: obj

    obj%name = "child"

end program main

```

As far as I know, an allocatable component is allowed to be defined in such a recursive way. And since the codes can be compiled with ifort, I guess it is gfortran’s bug that causes this failure. I want to know if there is some workaround when using gfortran. Or if I should define the derived types in an alternative way?

---

<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:** [March 21, 2022, 7:35pm UTC](https://fortran-lang.discourse.group/t/polymorphic-array-in-a-derived-type/3011/2 "2022-03-21T19:35:21Z")

</div>

Did you try this simple case? If I recall correctly, gfortran - for which a lot of such support started with the Fortran 2003 standard revision - does not fully support what is essentially a Fortran 2008 feature that is `allocatable component of a recursive type` (including polymorphic):

```fortran
   type :: t
      class(t), allocatable :: a
   end type
end 

```

---

<div class="post-metadata">

**Author:** ![plevold](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/plevold/32/754_2.png) [@plevold](https://fortran-lang.discourse.group/u/plevold)\
**Post date:** [March 21, 2022, 8:11pm UTC](https://fortran-lang.discourse.group/t/polymorphic-array-in-a-derived-type/3011/3 "2022-03-21T20:11:44Z")

</div>

As a workaround for gfortran not supporting recursive types you can use an unlimited polymorfic insted, e.g. `class(*), allocatable :: val` in `base_wrapper`. When using the value you’ll need to use `select type` to match against `base`. Clumsy and inefficient, but at least it doesn’t crash the compiler…

---

<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:** [March 22, 2022, 3:43am UTC](https://fortran-lang.discourse.group/t/polymorphic-array-in-a-derived-type/3011/4 "2022-03-22T03:43:03Z")

</div>

With @FortranFan’s case, gfortran (11.2.0) gives an internal compiler error so this is most likely something that is supposed to be supported but just doesn’t work. If it was an unsupported feature the compiler would report that or give some other normal source error. Unless this has been fixed on the development trunk, it ought to be reported to the gfortran bugzilla.

---

<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:** [March 22, 2022, 1:24pm UTC](https://fortran-lang.discourse.group/t/polymorphic-array-in-a-derived-type/3011/5 "2022-03-22T13:24:51Z")

</div>

> [@plevold](#):
>
> As a workaround for gfortran not supporting recursive types you can use an unlimited polymorfic insted …

Generally that is not advisable, the use of unlimited polymorphic types is best avoided, or at the very least kept at a minimum to those situations where the author knows exactly what she is doing and the Fortran processors are robust enough for the author’s intentions (which is not the case now).

Under the circumstances at present, it will be better to employ a “root” abstract “class” for the other “classes” one is working with including the recursive derived types of interest here. I expect `gfortran` to be able to support the following:

```fortran
module m

   type, abstract :: root_t
   end type

   type :: base_wrapper !<-- or even type, extends(root_t) :: base_wrapper
     class(root_t), allocatable :: val
   end type

   type, extends(root_t) :: base
     character(len=10) :: name
     type(base_wrapper), allocatable :: list(:)
   end type

   type, extends(base) :: child
   end type

end module

   use m

   type(child) :: obj
   obj%name = "child"

end

```

---

<div class="post-metadata">

**Author:** ![St\_Maxwell](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@St\_Maxwell](https://fortran-lang.discourse.group/u/St_Maxwell)\
**Post date:** [March 22, 2022, 3:51pm UTC](https://fortran-lang.discourse.group/t/polymorphic-array-in-a-derived-type/3011/6 "2022-03-22T15:51:35Z")

</div>

> [@FortranFan](#):
>
> ```auto
> type :: t
> class(t), allocatable :: a
> end type
> end 
> 
> ```

Yes, this piece of code does not compile in gfortran.

> [@FortranFan](#):
>
> ```auto
> module m
> 
> type, abstract :: root_t
> end type
> 
> type :: base_wrapper !<-- or even type, extends(root_t) :: base_wrapper
> class(root_t), allocatable :: val
> end type
> 
> type, extends(root_t) :: base
> character(len=10) :: name
> type(base_wrapper), allocatable :: list(:)
> end type
> 
> type, extends(base) :: child
> end type
> 
> end module
> 
> use m
> 
> type(child) :: obj
> obj%name = "child"
> 
> end
> 
> ```

And this works in gfortran.

I have just read the code of an XML library ([xml-fortran/tree\_struct.f90 in paulromano/xml-fortran](https://github.com/paulromano/xml-fortran/blob/master/src/tree_struct.f90)). It uses `pointer` instead of `allocatable` attribute. So I tried a pointer implementation for the code I posted, and it compiles in gfortran. I think it might be a feasible way too, although I am less willing to use pointer in general.
