[fpc-pascal] Feature announcement: Dynamic array extensions

Sven Barth pascaldragon at googlemail.com
Sun May 20 14:23:25 CEST 2018


Hello together!

I'm pleased to announce that after nearly a year various extensions for 
dynamic arrays have been finished. This includes the following features:

- support for array constructors using "[...]" syntax
- support for Insert(), Delete() and Concat()
- support for "+" operator
- support for dynamic array constants (and variable initializations)

In the following the features will be explained in detail:

## Array Constructors

It is now possible to initialize a dynamic array variable or parameter 
using the "[...]" which is also known from open array parameters. Unlike 
the ArrayType.Create() syntax which had been introduced with 3.0 this 
also works with unnamed array types. Nested dynamic arrays are also 
supported.

=== code begin ===

var
   t: array of LongInt;
   t2: array of array of LongInt;
begin
   t := [1, 2, 3, 4];
   t2 := [[1, 2, 3], [4, 5]];
end.

=== code end ===

## Insert(), Delete(), Concat()

The intrinsics Insert(), Delete() and Concat() can now be used with 
dynamic arrays as well.

Insert() allows to insert a single element, a dynamic array or a static 
array into a dynamic array at a specific index (zero based). If the 
dynamic array is empty it will simply contain the content to be 
inserted. If the index to insert is larger than the array's High() then 
the content will be added at the end.

Delete() allows to remove a range of elements from a dynamic array (the 
start index is zero based). The range will be capped to the array's 
boundaries.

Concat() allows to concatenate two (or more) dynamic arrays together. 
The second array content starts directly after the first and those of 
the third after the second and so on.

## "+" operator

The compiler now implements a "+" operator for arrays which is the same 
as if Concat() would be called on the arrays.

Note regarding backwards compatibility: existing "+" operator overloads 
for dynamic arrays no longer compile.

## Dynamic array constants

It is now possible to initialize dynamic array constants and variables 
with concrete contents in contrast to merely Nil. The syntax for this is 
the same as for static array constant and variable initializations.

=== code begin ===

const
   Test1: array of LongInt = (1, 2, 3);
var
   Test2: array of String = ('Alpha', 'Beta', 'Gamma');

=== code end ===

For constants the dynamic array constants adhere to the writable 
constants switch $J. E.g. the following will result in an exception:

=== code begin ===

{$J-}
const
   Test1: array of LongInt  = (1, 2, 3);
begin
   Test1[1] := 42;
end.

=== code end ===

It is possible to nest dynamic and static array constants.

Delphi compatibility:

- in Delphi modes the syntax for dynamic array constant and variable 
initialization is "[...]" instead of "(...)"
- Delphi does not adhere to the writable constants switch regarding the 
array's contents (so the example above - when adjusted for Delphi's 
syntax - will not result in an exception)
- Delphi does not correctly parse static array constants inside dynamic 
arrays; as we don't know what the syntax would be for Delphi then, this 
is prohibited in Delphi modes

Regards,
Sven


More information about the fpc-pascal mailing list