Jump to content
Stefan Glienke

Open array parameters and subranges

Recommended Posts

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:

 

OpenArrayCapture.thumb.PNG.2d7a5e79dacd28ff7e3a0b576b281467.PNG

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 :classic_sad:

  • Like 2

Share this post


Link to post

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 by Stefan Glienke
  • Like 1

Share this post


Link to post
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 by Mahdi Safsafi

Share this post


Link to post
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
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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×