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:

  • 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.

“new” keyword and “default” constructors

DWScript SVN version just introduced support for the “new” keyword and “default” constructors.

The syntax is similar to that of Oxygene/Prism, you can now create a new instance with

obj1 := new TSomeObject;
obj2 := new TSomeOtherObject(param1, param2);

By default, the above syntax will be duck-typed to the .Create constructor of a given class, but you can alternatively specify a “default” constructor (one per class) to select a specific named constructor that “new” will use:

type TSomeClass = class
   constructor ImTheOne; default;
end;

The Oxygene syntax is also extended in two ways, you can use “new” on a metaclass variable:

type TSomeObjectClass = class of TSomeObject;
...
var ref : TSomeObjectClass;
...
obj := new ref;

And you can further use it on an expression that returns a metaclass by placing it between brackets:

function TOtherClass.GetMetaClass : TSomeObjectClass;
...
var o : TOtherClass;
...
obj := new (o.GetMetaClass)(param1, param2);

The brackets are not just there to make it look like Turbo Pascal-era code, they are required for disambiguation, f.i. the two lines

obj1 := new TSomeClass(param);
obj2 := new (TSomeClass(param));

are compiled as a syntax variation of

obj1 := TSomeClass.Create(param);
obj2 := TSomeClass(param).Create;

The “new” keyword is also planned to be used as the basis of dynamic array instantiation.

Faster compiler, constant records, $include_once

Executive summary of recent DWS changes from the bleeding edge:

  • Compiler/parser performance improved by about 15%.
  • Added support for constant records declaration.
  • The $include_once directive is now supported, it will include a file only if it hasn’t been already included (be it by a previous $include_once or a regular $include).
  • Fixed some issues with script-side Exception.StackTrace method for wrapped Delphi-side exceptions.
  • Fixed-size arrays accesses with constant indexes are now checked at compile-time.
  • Multiple minor fixes, refactorings, minor optimizations and unit test coverage improvements.

Something “big” is also brewing in the DWScript lab, if not quite ready for prime-time just yet 😉