- DelphiTools - https://www.delphitools.info -

Dynamic arrays for DWScript

SVN version of DWScript adds a long-missing functionality in DWS: dynamic arrays. They provide a language-based alternative to the list and collections classes that had to be used so far.

They extend the “new” keyword for instantiation, and introduce pseudo-method semantics in addition to the traditional semantics for Low(), High() and Length().

var a : array of Integer;
var i : Integer;

a := new Integer[10];

for i := a.Low to a.High do
   a[i] := i;

The pseudo-methods currently available on dynamic arrays are:

Dynamic arrays in DWS are pure reference types and they behave a bit like a TList<T> would in Delphi, as SetLength() is a method, which modifies the array, rather than a global procedure, which can create a new copy of the array  (as in Delphi), ie. in DWScript, if you have:

var a, b : array of Integer;

a := new Integer[5];
b := a;
a[1] := 1;
PrintLn( b[1] );
a.SetLength(10);
a[1] := 2;
PrintLn( b[1] );

It will print 1 and 2. A Delphi version using “SetLength(a, 10)” instead would print 1 and 1.

In other words, in DWScript, if you want a new dynamic array instance, you use “new”, if want to make a copy of a dynamic array, you have to use .Copy(), if you want to resize, you use .SetLength(). Whereas in classic Delphi, all three aspects are behavioral variants of the SetLength() global procedure.

Note that if the dynamic arrays in DWS currently rely on compiler magic, there is a long term goal of having them mappable to a “regular” generic container class.