title: Delphi 2006 - Assigning dynamic array function results
body: <p>I had code with the basic constructs illustrated by the following:</p>
  
  <pre><code>type
      TDynamicArray = array of double ;
  
  var
      a : TDynamicArray ;
  
      function Func : TDynamicArray ;
  
      var
          b : TDynamicArray ;
  
      begin
      SetLength (B, 3) ;
      b [0] := 0.0 ;
      b [1] := 1.0 ;
      b [2] := 2.0 ;
      Result := b ;   // note 1  -- should use Result := Copy (b, 0, Length (b)) 
      end ;
  
  begin
  a := Func ;  // note 2  -- should we (or could we) use a := Copy (Func, 0, Length (Func)) 
  end.
  </code></pre>
  
  <p>It had been working fine until lately when the function started returning empty arrays.  I then spot <a href="https://stackoverflow.com/questions/3545449/is-there-way-of-copying-the-whole-array-into-another-array-other-than-using-a">this</a> which enlightens me to the fact that simple assignment isn't correct, I need to use <code>Copy</code>.</p>
  
  <p>Two questions:</p>
  
  <ul>
  <li>I realise I need 'Copy' on the line marked <code>Note 1</code> to assign to the
  function result.  Do I need to use <code>Copy</code> also on the assignment of
  the function result to array <code>a</code> (line <code>Note 2</code>)?.</li>
  <li>Why did the compiler allow my construct and what was the compiled code actually
  doing?</li>
  </ul>
  
  <p>I realise I could try these things and see, but I'm a little spooked by the compiler letting questionable stuff through.</p>