Not agreed in this case. The documentation does not tell me to initialise the argument of Setlength before using it. SetLength itself does the initialisation.
Well the issue, as far as I understand it, is:
Dynamic arrays and strings are so called managed types. They are always in some kind of initialized state (i.e. either nil, or valid data (old data can be valid)).
Those types are refcounted (they are pointers to data that includes a refcount). If a dynamic array goes out of scope, its refcount is reduced, and it is freed.
so in case of "var a: array of ...", a will be "nil" to start with (in practice, not necessarily documented) . Because there is no old data available, so there is only nil. Then SetLength will initialize the entries, because all entries are new.
Doing
SetLength(a,2);
a[1]:=99;
SetLength(a,20);
will keep the value of a[1].
In other words:
- SetLenght initializes new entries in the array
- SetLenght does NOT initialize the array itself
TIntegerArray = array of integer;
Procedure Foo(var a: TIntegerArray)
var param, will get the value passed in. SetLength will keep existing values.
And here is the final bit, that is less known.
For managed types "Result" is internally handled as a var param.
-------------------------
That is the technical bit.
As for what the expected behaviour should be.... I haven't checked all the docs.
But in general, ALL variables should be initialized before use.
I am not aware that managed types are excluded in the doc (in practice they often are, but its about the doc)
As above, SetLenght initializes the entries, not the array.
So initialization would be
Result := nil;
Result := SomeOtherArray;
--- EDIT
https://www.freepascal.org/docs-html/rtl/system/setlength.htmlDoesn't say anything about initializing the entries either.