XE7’s new dynamic array syntax
Good thing: Empty arrays can be used as default param value.
procedure Test(const Params: TVarArray = []);
Bad thing: Variants not fully supported?
Try uncommenting CVarArr in the example below.
Doesn’t work and gives “Constant expression expected”.
Yet the same constant list can be assigned to VVarArr
Go figure.
program XE7Tests;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils, Variants;
type
TVarArray = array of Variant;
TIntArray = array of Integer;
TTest = class
procedure Test(const Params: TVarArray = []);
end;
{ TTest }
procedure TTest.Test(const Params: TVarArray);
var
npar: Integer;
begin
npar := High(Params) – Low(Params) + 1;
Writeln(nPar);
end;
const
CIntArr: TIntArray = [1,2,3];
// CVarArr: TVarArray = [1,’2′, 3]; // “Constant expression expected”
var
Test: TTest;
VVarArr : TVarArray;
VIntArr : TIntArray;
begin
Test := TTest.Create;
try
try
Test.Test; // 0
Test.Test(nil); // 0
Test.Test([1,’2′, 3]); // 3
VVarArr := [1,’2′, 3, 3.14];
Test.Test(VVarArr); // 4
Test.Test(VVarArr + [‘five’]); // 5
// Test.Test(VVarArr + CVarArr); // Should have been 7
except
on E: Exception do
Writeln(E.ClassName, ‘: ‘, E.Message);
end;
finally
Test.Free;
Write(‘ – Enter: ‘);
Readln;
end;
end.
#XE7
You must be logged in to post a comment.