<div dir="auto"><div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Ryan Joseph via fpc-pascal <<a href="mailto:fpc-pascal@lists.freepascal.org" target="_blank" rel="noreferrer">fpc-pascal@lists.freepascal.org</a>> schrieb am Fr., 3. Apr. 2020, 21:03:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
<br>
> On Apr 3, 2020, at 10:09 PM, Sven Barth <<a href="mailto:pascaldragon@googlemail.com" rel="noreferrer noreferrer" target="_blank">pascaldragon@googlemail.com</a>> wrote:<br>
> <br>
> If your DoThis would be declared with a dynamic array parameter (e.G. "specialize TArray<Integer>") then you'd in fact have a temporary dynamic array constructed on the heap. <br>
> <br>
> Also I didn't say that dynamic arrays can't be constant, but that they only are so if declared in a const section. <br>
> <br>
<br>
can you post an example of a const dynamic array? I tried and it thinks it's a set so I can't assign to an open array.<br></blockquote></div></div><div dir="auto"><br></div><div dir="auto">It needs to be a typed constant:</div><div dir="auto"><br></div><div dir="auto">const</div><div dir="auto"> MyArray: array of LongInt = (1, 2, 3);</div><div dir="auto"><br></div><div dir="auto">If the writable constants switch is off then this dynamic array won't be writable either. </div><div dir="auto"><br></div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Also that made me think, is it possible for an open array parameter to be written to if its source was a static array? You said it's a pointer so I wonder if it's possible to use them instead of passing a static array pointer with an additional parameter for the length.<br></blockquote></div></div><div dir="auto"><br></div><div dir="auto">Yes that is indeed the case though you'll have to declare the parameter as var as otherwise the compiler will create a copy on the stack which will be lost afterwards. </div><div dir="auto"><br></div><div dir="auto">Also open array *always* start at index 0. So if you pass a static array that's declared as 4..6 then your function will get an open array stsrting at 0 with length 3 with the element at 0 being the element at 4 of the static array. </div><div dir="auto"><br></div><div dir="auto">And as a special functionality open arrays allow slicing. That is if you have an open array parameter you can pass along parts of it to another open array:</div><div dir="auto"><br></div><div dir="auto">SomeFunc(MyArrayArg[3..High(MyArrayArg) - 3]);</div><div dir="auto"><br></div><div dir="auto">This will also work correctly with var/out open array parameters. </div><div dir="auto"><br></div><div dir="auto">Regards,</div><div dir="auto">Sven </div></div>