title: VHDL - Function/Procedure for any type of array
body: <h1>Question:</h1>
  
  <p>How does one declare, if possible, a function intended for a parameter of any type <code>T</code> where the only constraint on <code>T</code> is that it was defined as a 1D <code>array</code> as in</p>
  
  <pre><code>type T is array ( integer range &lt;&gt; ) of <i>a_random_type</i>;</code></pre>
  
  <p>where <code><i>a_random_type</i></code> can be any <code>type</code>.</p>
  
  <p>The following syntactically incorrect function is an example of what is desired</p>
  
  <pre><code>function measure_size_of_any_array ( a : <i><b>array ( integer range &lt;&gt; )</b></i> ) return natural is
      variable r : natural := 0;
  begin
      for i in a'range loop
          r := r + 1;
      end loop;
      return r;
  end function;</code></pre>
  
  <p>which could then be used on any <code>array</code></p>
  
  <pre><code>type natural_array is array ( integer range &lt;&gt; ) of natural;
  type stdlogv_array is array ( integer range &lt;&gt; ) of std_logic_vector;
  
  [...]
  
  variable some_natural_array : natural_array;
  variable some_stdlogv_array : stdlogv_array;
  
  [...]
  
  constant size_1 : natural := measure_size_of_any_array(some_natural_array);
  constant size_2 : natural := measure_size_of_any_array(some_stdlogv_array);
  </code></pre>
  
  <p>Obviously, this question is about the way of defining the function and not about the function itself: I am not looking for <code>a'length</code>.</p>
  
  <hr>
  
  <h1>Possible Solution:</h1>
  
  <p>From <a href="https://books.google.be/books?id=ETxLguPMEY0C&amp;pg" rel="nofollow noreferrer">Ashenden's <em>VHDL-2008: Just the New Stuff</em></a></p>
  
  <blockquote>
    <p>Generic types can be specified for subprograms.</p>
    
    <p>We can declare a <em>formal generic type</em> in a generic list in the following way:</p>
  
  <pre><code>type <i>indentifier</i></code></pre>
    
    <p>A function with a generic list takes the form:</p>
  
  <pre><code>function <i>indentifier</i>
      generic   ( ... )
      parameter ( ... ) return <i>result_type</i> is
      ... -- declarations
  begin
      ... -- statements
  end function <i>identifier</i></code></pre>
  </blockquote>
  
  <p>which would allow the following definition</p>
  
  <pre><code>function measure_size_of_any_array
      generic   ( type arr_type )
      parameter ( arr : arr_type );
  </code></pre>
  
  <p>and the following use</p>
  
  <pre><code>function measure_size_of_natural_array is new measure_size_of_any_array
      generic ( arr_type =&gt; natural_array );
  function measure_size_of_stdlogv_array is new measure_size_of_any_array
      generic ( arr_type =&gt; stdlogv_array );
  
  constant size_1 : natural := measure_size_of_natural_array(some_natural_array);
  constant size_2 : natural := measure_size_of_stdlogv_array(some_stdlogv_array);
  </code></pre>
  
  <p>This provides the desired behavior of sharing the body of the function among different calls, regardless of the type of the elements of the <code>array</code> but still requires an instantiated function (which can be as local as desired so it isn't that bad).</p>
  
  <p>Because of how little support is being provided for VHDL-2008 by the main vendors (the previous solution wasn't understood by the compilers I tried), a VHDL-87, -93 or -2002 solution will be preferred.</p>
  
  <h3>Comment after receiving the first answers:</h3>
  
  <p>The previous information is my attempt to find a way of writing a VHDL subprogram accepting any argument as long as it is an <code>array</code> (<em>i.e.</em> to answer the initial question). The expected answer isn't necessarily supposed to use the same approach (namely using VHDL-2008 generic subprograms)!</p>