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:
- Low, High, Length: lower bound, higher bound and item count respectively.
- SetLength(n): adjust the number of items of an array.
- Add(item) : add an item to an array.
- Delete(index[, count]) : deletes one or more items from an array.
- Copy([index[, count]]) : creates a new array that holds a (shallow) copy of the items, there are three forms:
- Copy() copies the whole array
- Copy(index) copies all items starting from index
- Copy(index, count) copies count items starting from index
- Swap(index1, index2) : swaps the items at the specified indexes
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.