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

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