Array constructor

Speaking of features which are sparse in documentation. There is an intersting and a bit useless feature in dynamic arrays laying around: array constructors.

Let’s check out this example:

type
  TMyArray = array of Integer;

var
  X: TMyArray;
begin
  X := TMyArray.Create(1, 2, 3);

  WriteLn(Length(X));
  ReadLn;
end;

This code will actually compile and run.
(May probably be a remnant of old .NET days)

You May Also Like

About the Author: Alexandru Ciobanu

5 Comments

  1. You know, that’s actually pretty cool. The syntax is weird, but it’s good to know that there’s actually a way to construct a dynamic array on the fly, without having to SetLength and then iterate.

  2. It’s a bit slower than doing it manually. It creates a temporary array, clears it, sets its values and then assigns that temporary reference to the variable you assign to.

  3. Hehe, fortunately I’ve skipped the D.NET period and switched to C# for a few years so these “syntax sweets” are new to me.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.