title: Dynamic typing or generics in Oracle PL/SQL
body: <p>For some reason, I have certain fields in a table, that are collections of chars. Chars of different length. Example:</p>
  
  <pre><code>create or replace type t_charray_1 as varray(5) of char(1);
  create or replace type t_charray_2 as varray(5) of char(2);
  create or replace type t_charray_3 as varray(5) of char(3);
  create or replace type t_charray_4 as varray(5) of char(4);
  
  create table mytable (
  field1 number,
  field2 t_charray_1,
  field3 t_charray_3,
  </code></pre>
  
  <p>Also, I have a function that returns a (fixed length) string representation of a <code>mytable</code> record. This function calls other functions that are returning the string representation of a given collection-typed field. Examples:</p>
  
  <pre><code>  function to_chr(
      p_array in t_charray_1, 
      pad_length in number, 
      p_list_length in number
    ) return char as
      v_res varchar2(255) := '';
    begin
      for i in 1 .. p_list_length loop
        if p_array is not null and p_array.exists(i) and p_array(i) is not null then
          v_res := v_res || rpad(p_array(i), pad_length, ' ');
        else
          v_res := v_res || rpad(' ', pad_length, ' ');
        end if;
      end loop;
      return v_res;
    end to_chr;
    ------------------------------------------------------------------------------
    function to_chr(
      p_array in t_charray_2, 
      pad_length in number, 
      p_list_length in number
    ) return char as
      v_res varchar2(255) := '';
    begin
      for i in 1 .. p_list_length loop
        if p_array is not null and p_array.exists(i) and p_array(i) is not null then
          v_res := v_res || rpad(p_array(i), pad_length, ' ');
        else
          v_res := v_res || rpad(' ', pad_length, ' ');
        end if;
      end loop;
      return v_res;
    end to_chr;
  </code></pre>
  
  <p>Note that these functions are <em>overloaded</em> versions of each other. The only difference in their signature is the type of the <code>p_array</code> argument.</p>
  
  <p>Please also note that <em>the bodies of these functions are identical</em>.</p>
  
  <p><strong>Motivation</strong></p>
  
  <p>I want to eliminate duplicate code. What are my choices?</p>
  
  <p><strong>EDIT</strong> I have heard of sys.anydata but never used it. Can it be a solution?</p>