# Class array with different types at each index

**URL:** <https://fortran-lang.discourse.group/t/class-array-with-different-types-at-each-index/2627>\
**Category:** Help\
**Created:** [January 21, 2022, 4:47pm UTC](https://fortran-lang.discourse.group/t/class-array-with-different-types-at-each-index/2627 "2022-01-21T16:47:35Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![milancurcic](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/milancurcic/32/2_2.png) [@milancurcic](https://fortran-lang.discourse.group/u/milancurcic)\
**Post date:** [July 19, 2023, 9:26pm UTC](https://fortran-lang.discourse.group/t/class-array-with-different-types-at-each-index/2627/21 "2023-07-19T21:26:28Z")

</div>

> [@HugoMVale](#):
>
> At first, when I saw these declarations:
> 
> ```auto
> type(method_a) :: a
> type(method_b) :: b
> 
> ```
> 
> I was confused. But then I realized it is just a typo and apparently not required.

You’re right–I forgot to remove them.

---

<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:** [July 20, 2023, 12:12am UTC](https://fortran-lang.discourse.group/t/class-array-with-different-types-at-each-index/2627/22 "2023-07-20T00:12:42Z")

</div>

> [@milancurcic](#):
>
> ```auto
> do i = 1, size(methods)
> select type (this_method => methods(i) % m)
> class is (method)
> call this_method % check(result)
> 
> ```

A good practice can be to strive to avoid `SELECT TYPE` altogether.

In this instance it’s not needed.

```fortran
program main
   use methods_module, only: method, method_a, method_b
   implicit none

   type :: method_wrapper
     class(method), allocatable :: m
   end type method_wrapper

   type(method_wrapper) :: methods(2)
   integer :: i
   logical :: result

   allocate(methods(1) % m, source=method_a())
   allocate(methods(2) % m, source=method_b())

   do i = 1, size(methods)
      call methods(i)%m%check(result)
      print *, i, result
   end do

end program main

```

```auto
C:\temp>gfortran -ffree-form p.f -o p.exe

C:\temp>p.exe
           1 T
           2 F

```

Also, consider `ELEMENTAL` procedures as much as feasible, even with type-bound.

---

<div class="post-metadata">

**Author:** ![milancurcic](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/milancurcic/32/2_2.png) [@milancurcic](https://fortran-lang.discourse.group/u/milancurcic)\
**Post date:** [July 20, 2023, 12:49am UTC](https://fortran-lang.discourse.group/t/class-array-with-different-types-at-each-index/2627/23 "2023-07-20T00:49:16Z")

</div>

I didn’t know you could do that; great! I may have a few spurious `select type`s in my code that I should revisit.

[Previous page](https://fortran-lang.discourse.group/t/class-array-with-different-types-at-each-index/2627.md?page=1)
