title: Write array elements based on element type
body: <p>What I want to accomplish is best explained by an example. In Fortran I have an array <code>a</code> of type <code>mixed</code> that holds a function pointer, an integer variable and a real variable.</p>
  
  <pre><code>type mixed
     procedure(), pointer, nopass :: f_ptr
     integer :: vi
     real :: vr
  end type
  
  type(mixed), dimension(10) :: a
  </code></pre>
  
  <p>The pointer <code>f_ptr</code> should point to an overloaded function that either returns the integer <code>vi</code> or the real <code>vr</code>. </p>
  
  <pre><code>function f_i(i)
     implicit none
     integer, intent(in) :: i
     integer :: f_i
     f_i = a(i)%vi
     return
  end function
  
  function f_r(i)
     implicit none
     integer, intent(in) :: i
     real :: f_r
     f_r = a(i)%vr
     return
  end function
  </code></pre>
  
  <p>The array is initialized by for example</p>
  
  <pre><code>a(1)%vi = 2
  a(1)%f_ptr =&gt; f_i
  a(2)%vr = 3.14
  a(2)%f_ptr =&gt; f_r
  ...
  a(10)%vr = 42
  a(10)%f_ptr =&gt; f_r
  </code></pre>
  
  <p>In a loop over a variable number of elements then I want to write the array elements <code>vi</code> or <code>vr</code> to an unformatted file via the function that <code>f_ptr</code> is pointing to.</p>
  
  <pre><code>n = 2
  write(lununf) (a(j)%f_ptr, j=1,n)
  </code></pre>
  
  <p>The (readable form) output should be</p>
  
  <pre><code>2     3.14
  </code></pre>
  
  <p>I tried to implement this but cannot find a solution. Can it be done in some way?</p>