XE7’s new dynamic array syntax

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.

 

3 thoughts on “XE7’s new dynamic array syntax


  1. The reason this doesn’t compile in XE7…


    const


      CVarArr: TVarArray = [1,’2′, 3];


    … is the same reason why this doesn’t compile in any version:


    const


      CVarArr: array[0..2] of Variant = (1, ‘2’, 3);


    Namely, the elements of a typed constant array must themselves be true constants, and you can’t declare a Variant true constant.

Leave a Reply