title: Creating heterogeneous arrays in Fortran
body: <p>I am trying to create heterogeneous arrays that contain variables of different types, for example, <code>[ 1.0, 7, "hi" ]</code>. I tried to include <code>class(*)</code> or <code>type(*)</code> in the array constructor (please see the end of the following code), but gfortran5.2 simply regards it as syntax error. Is there any way to make such an array with array constructor, or is it necessary to use a different approach (e.g., define a type that contains each element separately)?</p>
  
  <hr>
  
  <p>More details:</p>
  
  <p>The following code is an example why I want to create such an array. The <code>checktype_multi</code> routine receives multiple arguments with the <code>optional</code> keyword, but this approach is clearly limited because of the fixed number of arguments. To allow arbitrary number of arguments, I tried the <code>checktype_array</code> routine, but it seems not possible to pass an array with different types... A more practical <a href="https://stackoverflow.com/questions/30091152/print-and-write-in-one-line/33620573#33620573">case</a> may be to make a subroutine for printing a variable number of arguments with various types.</p>
  
  <pre><code>module mymod
      implicit none
  contains
  
      subroutine checktype ( x )
          class(*) :: x
  
          select type ( x )
              type is ( integer )      ; print *, "int    : ", x
              type is ( real )         ; print *, "real   : ", x
              type is ( character(*) ) ; print *, "string : ", x
          endselect
      end subroutine
  
      subroutine checktype_multi ( x1, x2, x3 )
          class(*), optional :: x1, x2, x3
  
          print *
          if ( present( x1 ) ) call checktype ( x1 )
          if ( present( x2 ) ) call checktype ( x2 )
          if ( present( x3 ) ) call checktype ( x3 )
      end subroutine
  
      subroutine checktype_array ( a )
          class(*) :: a(:)
          integer :: k
  
          print *
          do k = 1, size( a )
              call checktype ( a( k ) )
          enddo
      end subroutine
  
  end module
  
  program main
      use mymod
  
      call checktype_multi ( 1.0 )
      call checktype_multi ( 1.0, 7 )
      call checktype_multi ( 1.0, 7, "hi" )
  
      ! call checktype_array ( [ 1.0, 7, "hi" ] )  !! error (this is to be expected)
  
      !&gt;&gt;&gt; Here is the problem.
      ! call checktype_array ( [ type(*)  :: 1.0, 7, "hi" ] )  !! this is also an error
      ! call checktype_array ( [ class(*) :: 1.0, 7, "hi" ] )  !! this too
  end program
  </code></pre>