Stefan Glienke 2009 Posted September 7, 2020 How to use open array parameters to handle array subranges: https://delphisorcery.blogspot.com/2020/09/open-array-parameters-and-subranges.html 10 Share this post Link to post
Mahdi Safsafi 225 Posted September 8, 2020 Very nice article Stefan and the way you used Slice to avoid the copy is amazing ! Speaking of bugs, I found two drawback related to using open array param: First, I already wrote on this forum a topic about unnamed type and rtti. Compiler do not generate rtti for an unnamed type ! this is easy to avoid with most types because all what you need to do is to declare the type : type x = xxx. But for open array type that's impossible because the syntax conflicts with dynamic array syntax ! So I suppose there is no official way to have a correct rtti for a function that uses open array param. Second, On my D10.3, the TArray<T> example caused an "IDE/Compiler not responding" issue that always ends up by IDE restart: I was able to isolate the line that causes the issue : MergeSort(Slice(TOpenArray<T>((@values[mid])^), len - mid)); // if I comment this line everything works great ! I tried with different build mode (debug,release) and on different project and the result is always the same 2 Share this post Link to post
Stefan Glienke 2009 Posted September 8, 2020 (edited) Ugh, nasty sh*t - can repro in earlier versions than 10.4 as well. The compiler trips over the type inference - need to write MergeSort<T>(...) I would guess it spirals down into some endless loop trying to infer whatever comes back from that tricked Slice call. Updated the blog post - thanks Edited September 8, 2020 by Stefan Glienke 1 Share this post Link to post
Mahdi Safsafi 225 Posted September 8, 2020 (edited) 2 hours ago, Stefan Glienke said: Updated the blog post - thanks There is another way without specifying explicitly the generic type param and it generates the same output. Internally slice requires dst(open array type), src(pointer type) and of course a count(integer type). It uses src to get the pointer and dst info to compute the offset meaning you can provide a fake declaration for the source: type TOpenArray = array[0 .. 0] of Byte; POpenArray = ^TOpenArray ; MergeSort(Slice(POpenArray(@values[mid])^, len - mid)); Edited September 8, 2020 by Mahdi Safsafi Share this post Link to post
David Heffernan 2347 Posted September 8, 2020 1 hour ago, Mahdi Safsafi said: There is another way without specifying explicitly the generic type param and it generates the same output. Internally slice requires dst(open array type), src(pointer type) and of course a count(integer type). It uses src to get the pointer and dst info to compute the offset meaning you can provide a fake declaration for the source: type TOpenArray = array[0 .. 0] of Byte; POpenArray = ^TOpenArray ; MergeSort(Slice(POpenArray(@values[mid])^, len - mid)); What does this offer over what Stefan posted in his article? Share this post Link to post
Mahdi Safsafi 225 Posted September 8, 2020 16 minutes ago, David Heffernan said: What does this offer over what Stefan posted in his article? Typically both produce the same output. Stefan code is based on template. Mine is not. I found my code more readable and friendly for typing (you don't need to worry about <>). But its just a taste of manner. Share this post Link to post
David Heffernan 2347 Posted September 8, 2020 20 minutes ago, Mahdi Safsafi said: Typically both produce the same output. Stefan code is based on template. Mine is not. I found my code more readable and friendly for typing (you don't need to worry about <>). But its just a taste of manner. But your code can only be used for arrays of byte. Stefan is demonstrating generic code. Share this post Link to post