There is no reason to use Copy
in either of those assignment statements. Normal assignment works fine. When you allocate b
, the reference count of the array is 1. When you assign it to Result
, the reference count becomes 2. (Personally, I'd just forego b
and operate directly on Result
from the start.) When the function returns, b
goes out of scope and the reference count becomes 1 again. Finally, when the result is assigned to a
, nothing happens because Result
was actually an alias for a
all along. Ultimately, you're left with one unique array, which should be exactly what you intended.
The compiler allowed your code because your code is fine. The compiler recognizes assignments between arrays just as it does assignments between strings and assignments between interface references, and it generates the right code to adjust the reference counts accordingly.
Using Copy
would create an entire copy of the array, but you don't need that. Each time you think you need it, it's in a place where you would have immediately discarded the previous copy anyway. Why make a copy when you already have a perfectly good array ready for any use you want?