[fpc-devel] Maybe room for better documentation? open array as var param
Martin Frb
lazarus at mfriebe.de
Thu Jul 20 19:37:09 CEST 2023
On 20/07/2023 19:24, Michael Van Canneyt via fpc-devel wrote:
>
>
> It's IMO probably better to outright forbid passing open array by
> reference.
>
> printing length(a) after x:=Nil; gives 10, which is simply wrong.
The open array is not the same as the dyn array.
It is at any point just a slice of the dyn array. (even if the slice has
all elements of the dyn array).
At no point is it possible to do a SetLength on the open array (and
therefore the lenght of the dyn array can't be changed either - not via
the open array).
In otherwise the reference is only a reference to the members.
There is at no point a reference to the dyn array itself, nor to the length.
Of course if the underlaying data goes away, then the "var param" open
array (same as "const param") points to some memory....
I don't know if it needs to be forbidden (don't mind if).
But I think
- explicit documentation
- a compiler note or warning might be helpful
More information about the fpc-devel
mailing list